Dela via


SuspendingEventHandler Delegate

Definition

Represents the method that will handle the Suspending event.

public delegate void SuspendingEventHandler(Platform::Object ^ sender, SuspendingEventArgs ^ e);
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(591565925, 58218, 16610, 177, 57, 164, 112, 70, 2, 166, 225)]
class SuspendingEventHandler : MulticastDelegate
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(591565925, 58218, 16610, 177, 57, 164, 112, 70, 2, 166, 225)]
public delegate void SuspendingEventHandler(object sender, SuspendingEventArgs e);
Public Delegate Sub SuspendingEventHandler(sender As Object, e As SuspendingEventArgs)

Parameters

sender
Object

Platform::Object

IInspectable

The object where the handler is attached.

e
SuspendingEventArgs

Event data.

Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

This code example demonstrates a typical usage pattern for this event. This code is used by many of the XAML samples, for example the Input sample, as part of the code-behind for the app.xaml file. If you browse the XAML samples, you can find the source code for the SuspensionManager class API as referenced in this code.

async protected void OnSuspending(object sender, SuspendingEventArgs args)
{
    SuspendingDeferral deferral = args.SuspendingOperation.GetDeferral();
    await SuspensionManager.SaveAsync();
    deferral.Complete();
}
Private Async Sub OnSuspending(sender As Object, args As SuspendingEventArgs) 
    Dim deferral As SuspendingDeferral = args.SuspendingOperation.GetDeferral 
    Await SuspensionManager.SaveAsync 
    deferral.Complete() 
End Sub 

Remarks

The system suspends your app whenever the user switches to another app or to the desktop, and resumes your app whenever the user switches back to it. However, the system can also terminate your app while it is suspended in order to free up resources. Therefore, you should handle the Suspending event to perform the following operations:

  • Preserve user session state.
  • Release any exclusive locks on resources.
  • Reduce memory usage if possible. For example, serialize any data that is easy to reconstruct in object form upon reactivation.
  • Save app state. The Suspending event is the only indication your app will receive prior to termination (if it happens). Because of this, you should store enough session state (such as the current article being read or the current movie playback position) to recreate the exact same experience during activation. The guidance for content creation apps is to save a user’s work early and often but also commit one final save during Suspending. Saving data prior to suspension is useful because the Suspending event handler has only 5 seconds to complete its operation.

If your app is terminated, you can restore the app state in an OnLaunched method override. If your app resumes before it is terminated, the system restores the app state automatically. You should handle the Resuming event only if you need to refresh any displayed content that might have changed while the app is suspended, such as news feeds or the user's location.

Applies to