Handle cancellation by exiting your background task TaskInstance->Canceled += ref new BackgroundTaskCanceledEventHandler(this, &ExampleBackgroundTask::OnCanceled) taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled) For example, use the following line of code. In an in-process background task, you might do this registration as part of your application initialization. In the background task's Run method, register the OnCanceled event handler method before starting work. Void ExampleBackgroundTask::OnCanceled(IBackgroundTaskInstance^ taskInstance, BackgroundTaskCancellationReason reason) Indicate that the background task is canceled.ĭebug.WriteLine("Background " + + " Cancel Requested.") private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) The full background task sample OnCanceled method sets _CancelRequested to true and writes potentially useful debug output. In the OnCanceled method you created in step 1, set the flag variable _CancelRequested to true. This variable will be used to indicate when a cancellation request has been made. Windows::ApplicationModel::Background::BackgroundTaskCancellationReason reason)Īdd a flag variable called _CancelRequested to the background task class. Windows::ApplicationModel::Background::IBackgroundTaskInstance const& taskInstance, TODO: Add code to notify the background task that it is cancelled. This method is the entry point called by the Windows Runtime when a cancellation request is made against your background task. Your background task should be designed to handle this scenario.Ĭreate a method named OnCanceled as follows. This helps to ensure the user experience of the app in the foreground. If an out of memory exception is not surfaced, or the app doesn't handle it, then the background task will be terminated without warning and without raising the OnCanceled event. Write a method to handle the cancellation event.įor all device families except desktop, if the device becomes low on memory, background tasks may be terminated. Use the OnCanceled method to recognize cancellation requests In-process background tasks do not require you to use persistent storage to signal the cancellation because you can communicate the cancellation using app state since the background task is running in the same process as your foreground app. But instead of the Run method, substitute OnBackgroundActivated. This topic is also applicable to in-process background tasks. For more in-depth information on conditions and triggers, see Support your app with background tasks. To get started quickly building a background task, see Create and register an out-of-process background task or Create and register an in-process background task. This topic assumes you have already created a background task class, including the Run method that is used as the background task entry point. Learn how to make a background task that recognizes a cancellation request, stops work, and reports the cancellation to the app using persistent storage.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |