You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hub/apps/windows-app-sdk/applifecycle/background-tasks.md
+22-2Lines changed: 22 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,17 @@ Background tasks are app components that run in the background without a user in
16
16
17
17
The implementation of background tasks is different for UWP and WinUI apps. For information about migrating your UWP apps with background tasks to WinUI, see the Windows App SDK [Background task migration strategy](../migrate-to-windows-app-sdk/guides/background-task-migration-strategy.md).
18
18
19
+
[Task Scheduler](/windows/win32/api/_taskschd/) helps desktop apps achieve the same functionality that is provided by [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) in UWP apps. More details on implementations using **TaskScheduler** are available [here](/windows/win32/api/taskschd/).
20
+
19
21
## Register a background task
20
22
21
23
Use the [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder) class included with the Windows App SDK to register a background task that uses full trust COM component.
22
24
23
25
The following example shows how to register a background task using C++. In the Windows App SDK github sample, you can see this registration code in [MainWindow.Xaml.cpp](https://github.com/microsoft/WindowsAppSDK-Samples/blob/main/Samples/BackgroundTask/InProc%20BackgroundTask/cpp-winui/BackgroundTaskBuilder/MainWindow.xaml.cpp#L82)
24
26
25
27
```cpp
28
+
auto access = co_awaitBackgroundExecutionManager::RequestAccessAsync();
29
+
26
30
// Unregister all existing background task registrations
27
31
auto allRegistrations = BackgroundTaskRegistration::AllTasks();
28
32
for (constauto& taskPair : allRegistrations)
@@ -33,17 +37,28 @@ for (const auto& taskPair : allRegistrations)
33
37
34
38
//Using the Windows App SDK API for BackgroundTaskBuilder
The following example shows how to register a background task using C#. In the Windows App SDK github sample, you can see this registration code in [MainWindow.Xaml.cpp](https://github.com/microsoft/WindowsAppSDK-Samples/blob/main/Samples/BackgroundTask/InProc%20BackgroundTask/cs-winui/BackgroundTaskBuilder/MainWindow.xaml.cs#L79).
@@ -68,8 +84,12 @@ Note that the call to [SetEntryPointClsid](/windows/windows-app-sdk/api/winrt/mi
68
84
69
85
Use the following best practices when registering background tasks.
70
86
87
+
* Call [BackgroundExecutionManager.RequestAccessAsync](/uwp/api/windows.applicationmodel.background.backgroundexecutionmanager.requestaccessasync) before registering background tasks.
88
+
71
89
* Don't register a background task multiple times. Either verify that a background task isn't already registered before registering or, as in the Windows App SDK sample, unregister all background tasks and then reregister the tasks. Use the [BackgroundTaskRegistration](/uwp/api/windows.applicationmodel.background.backgroundtaskregistration) class to query for existing background tasks.
72
90
91
+
* Use the [BackgroundTaskBuilder.Name](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder.name) property to specify a meaningful name for the background task to simplify debugging and maintenance.
0 commit comments