21 July 2015

Windows Store App - Saving data on suspending

In my app I have a method WriteBinaryDataFile, that is called when the app is suspended. First it writes Started writing to the output window, then does it's work, and finishes by outputting Finished writing. Interestingly, Started writing appeared, but Finished writing didn't, exept when I terminated the app manually. It turned out that the method needs to be async, and had to be awaited. The code is quite simple:
public async Task WriteBinaryDataFile()
{
    //Do what needs to be done.
}

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();
    await MainPage.WriteBinaryDataFile(); //Await and in the middle.
    deferral.Complete();
}

No comments:

Post a Comment