SuspendingEventHandler delegate

Applies to Windows and Windows Phone

Represents the method that will handle the Suspending event.

Syntax

Public Delegate Sub SuspendingEventHandler( 
  sender As Object,  
  e As SuspendingEventArgs 
)

Attributes

[Version(0x06020000)]
[WebHostHidden()]

Parameters

sender

Type: System.Object [.NET] | Platform::Object [C++]

The object where the handler is attached.

e

Type: SuspendingEventArgs

Event data.

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.

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 APIs as referenced in this code.

Private Async Sub OnSuspending(sender As Object, args As SuspendingEventArgs) 
    Dim deferral As SuspendingDeferral = args.SuspendingOperation.GetDeferral 
    Await SuspensionManager.SaveAsync 
    deferral.Complete() 
End Sub 

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

 

 

Show:
© 2014 Microsoft