Friday, August 30, 2013

WinRT Cascading async Calls


private async void engine_SaveRequested(object sender, SaveRestoreEventArgs e)
{
await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
//create a Saved Game folder for saving all the games to app local folder or open existing folder
var savedGameFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SavedGame", CreationCollisionOption.OpenIfExists);
//set currently saved game folder name
string gameFolderName = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".save";
//create folder for currently saved game in the SavedGame folder and append a integer value if file name exists
var currentGameFolder = await savedGameFolder.CreateFolderAsync(gameFolderName, CreationCollisionOption.GenerateUniqueName);
//create currently saved game file in the currentGameFolder with the gameFolderName
var savedGameFile = await currentGameFolder.CreateFileAsync(gameFolderName, CreationCollisionOption.ReplaceExisting);
//open savedGameFile stream for writing
var stream = await savedGameFile.OpenStreamForWriteAsync();
e.Stream = stream;
});
}

The above code does not compile.


I've tried to work through it a number of ways, but a solution escapes me.


The point of the function (called from a non-UI task thread) is to do the steps as a transaction. Each step in order with a result so that e.Stream has a file Stream.


I can do this if I block the UI, but I don't want to block. I want it to just run through the statements as they are...I don't care if each statement is asynchronous as long as they run in order and don't end the engine_SaveRequested method until everything is done.


Help?


Dave


No comments:

Post a Comment