Does the in-process background task, defined via the OnBackgroundActivated
method, run even if the main application is closed or suspended or must I implement an out-of-process background task?
The documentation is not clear on this.
- Support your app with background tasks
- Create and register an out-of-process background task
- Create and register an in-process background task
I've written out-of-process background tasks before and they run even when the app is not. However, it seems to me that an in-process background task won't run unless the app is active. I've used a deferral, as suggested in the documentation, to avoid the task being closed, and I've set the oneShot
to false
when registering the task. The task takes no longer than 10 seconds. The task is registered and can be run manually from Visual Studio, but doesn't seem to run automatically, if the app is closed.
protected async override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
base.OnBackgroundActivated(args);
IBackgroundTaskInstance taskInstance = args.TaskInstance;
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
await RunBackgroundWork();
deferral.Complete();
}
In summary, if you need to create a background task that should run even if the main app is not open, can this be done with an in-process background task, or must an out-of-process background task be used?