Syntax Error

Code samples and hints

Time limit on suspending a WinRT app

When a WinRT Metro Style app gets suspended, you have to save your user state for the case that the app will be terminated later. 

You have a maximum time limit of 5 seconds to finish your work. After this time the app is closed without any further actions.
First tell the system, that you want to do something with the SuspendingOperation GetDeferral-Method. After your save actions, finish your work with Complete.

/// <summary>
/// Invoked when application execution is being suspended.  Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
async void OnSuspending(object sender, SuspendingEventArgs e)
{
            var waitState = e.SuspendingOperation.GetDeferral();

            Debug.WriteLine("<------------ Persist Data");
            await DoMyPersistLocalSettings(); 
            await DoMyPersistLocalCache<DataModel>();
            Debug.WriteLine("Persist Data ------------>");

            waitState.Complete();
}

 

Warning: Even on debugging the 5 second time limit is enabled. Don't wait here or step manually through your lines of code.

Kommentare sind geschlossen