Skip to content

Commit 2c0ca77

Browse files
Merge pull request #5208 from MicrosoftDocs/alvinashcraft/main-winappsdk-background-tasks
Do Not Merge - Create new overview with samples for BackgroundTaskManager APIs in WinAppSDK 1.7
2 parents 488b5a7 + ac40dd9 commit 2c0ca77

File tree

8 files changed

+183
-6
lines changed

8 files changed

+183
-6
lines changed

hub/apps/develop/app-lifecycle-and-system-services.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
2-
title: App lifecycle and system services
2+
title: App lifecycle, background tasks, and system services in Windows apps
33
description: This article provides an index of development features that are related to scenarios involving managing the lifecycle of Windows apps and system-level services.
4+
ms.date: 02/13/2025
45
ms.topic: concept-article
5-
ms.date: 02/12/2025
6+
keywords: windows 11, winui, background task, app service, connected devices, remote systems, app lifecycle
67
ms.localizationpriority: medium
7-
# customer-intent: As a Windows developer, I want to learn about the features related to Windows app lifecycle and related system services.
8+
# Customer intent: As a Windows developer, I want to learn about the features available for managing the lifecycle of Windows apps and using system-level services.
89
---
910

10-
# App lifecycle and system services
11+
# App lifecycle, background tasks, and system services
1112

1213
This article provides an index of development features that are related to scenarios involving managing the lifecycle of Windows apps and using system-level services provided by the Windows OS.
1314

@@ -21,8 +22,9 @@ The [Windows App SDK](../windows-app-sdk/index.md) provides the following featur
2122
|---------|-------------|
2223
| [App lifecycle](../windows-app-sdk/applifecycle/applifecycle.md) | Get an overview of managing the lifecycle of your app. |
2324
| [App instancing](../windows-app-sdk/applifecycle/applifecycle-instancing.md) | Control whether multiple instances of your app's process can run at the same time. |
25+
| [Background tasks](../windows-app-sdk/applifecycle/background-tasks.md) | Background tasks are app components that run in the background without a user interface. They can perform actions such as downloading files, syncing data, sending notifications, or updating tiles. |
2426
| [Rich activation](../windows-app-sdk/applifecycle/applifecycle-rich-activation.md) | Receive information about different kinds activations for your app. |
25-
| [Power management](../windows-app-sdk/applifecycle/applifecycle-power.md) | Get visibility into how your app affects the device's power state, and enable your app to make intelligent decisions about resource usage.
27+
| [Power management](../windows-app-sdk/applifecycle/applifecycle-power.md) | Get visibility into how your app affects the device's power state, and enable your app to make intelligent decisions about resource usage. |
2628
| [Restart](../windows-app-sdk/applifecycle/applifecycle-restart.md) | Programmatically restart your application and set restart options after app termination. |
2729

2830
## Windows OS features

hub/apps/develop/launch/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This section describes how to manage background tasks in your app.
4444
| Topic | Description |
4545
|-------|-------------|
4646
| [Working with background tasks in Windows apps](create-and-register-a-background-task.md) | Learn how to create and register a background task in your app with the Windows Runtime (WinRT) [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) class. |
47+
| [BackgroundTaskBuilder in the Windows App SDK](../../windows-app-sdk/applifecycle/background-tasks.md) | Learn how to create and register a background task in a WinUI app with the Windows App SDK BackgroundTaskBuilder APIs. |
4748

4849
## Related content
4950

hub/apps/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ items:
247247
items:
248248
- name: Working with background tasks in Windows apps
249249
href: develop/launch/create-and-register-a-background-task.md
250+
- name: BackgroundTaskBuilder in the Windows App SDK
251+
href: windows-app-sdk/applifecycle/background-tasks.md
250252
- name: Notifications
251253
items:
252254
- name: Push notifications
@@ -1111,6 +1113,8 @@ items:
11111113
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/dwritecore.md
11121114
- name: MRT to MRTCore migration
11131115
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/mrtcore.md
1116+
- name: Background task migration strategy
1117+
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/background-task-migration-strategy.md
11141118
- name: Threading functionality migration
11151119
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/threading.md
11161120
- name: Case study 1—PhotoLab (C#)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Using background tasks in Windows apps
3+
description: Get an overview of using background tasks and learn how to create a new background task in an app with the BackgroundTaskBuilder in Windows App SDK.
4+
ms.date: 03/19/2025
5+
ms.topic: concept-article
6+
keywords: windows 11, winui, background task, app lifecycle, windows app sdk
7+
ms.localizationpriority: medium
8+
# customer intent: As a Windows developer, I want to learn about using background tasks in Windows apps.
9+
---
10+
11+
# Using background tasks in Windows apps
12+
13+
This article provides an overview of using background tasks and describes how to create a new background task in a WinUI 3 app. 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).
14+
15+
## BackgroundTaskBuilder in the Windows App SDK
16+
17+
Background tasks are app components that run in the background without a user interface. They can perform actions such as downloading files, syncing data, sending notifications, or updating tiles. They can be triggered by various events, such as time, system changes, user actions, or push notifications. These tasks can get executed when corresponding trigger occurs even when the app is not in running state.
18+
19+
The Windows Runtime (WinRT) [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) was designed for UWP applications, and many of the background task triggers are not supported for full trust COM Components. They are supported only when registered with WinRT components that are launched with a `backgroundtaskhost` process. Due to this, Windows App SDK desktop applications can't directly register the full trust COM components to be launched with background task triggers. They require a workaround of including the WinRT components in the project. The [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder) in Windows App SDK API avoids this workaround so WinUI 3 and other desktop applications that use Windows App SDK can register the full trust COM components directly with background tasks.
20+
21+
## Register a background task
22+
23+
The following example registers a background task for a full trust COM component using the Windows App SDK [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder). See the [Background task migration strategy](../migrate-to-windows-app-sdk/guides/background-task-migration-strategy.md) guide for more information.
24+
25+
The C++ code to create and register a background task is as follows:
26+
27+
```cpp
28+
//Using the Windows App SDK API for BackgroundTaskBuilder
29+
winrt::Microsoft::Windows::ApplicationModel::Background::BackgroundTaskBuilder builder;
30+
SystemTrigger trigger = SystemTrigger(SystemTriggerType::TimeZoneChange, false);
31+
auto backgroundTrigger = trigger.as<IBackgroundTrigger>();
32+
builder.SetTrigger(backgroundTrigger);
33+
builder.AddCondition(SystemCondition(SystemConditionType::InternetAvailable));
34+
builder.SetTaskEntryPointClsid(classGuid);
35+
builder.Register();
36+
```
37+
38+
To create and register the background task in C#, the code is as follows:
39+
40+
```csharp
41+
//Using the Windows App SDK API for BackgroundTaskBuilder
42+
var builder = new Microsoft.Windows.ApplicationModel.Background.BackgroundTaskBuilder();
43+
var trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
44+
var backgroundTrigger = trigger as IBackgroundTrigger;
45+
builder.SetTrigger(backgroundTrigger);
46+
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
47+
builder.SetTaskEntryPointClsid(classGuid);
48+
builder.Register();
49+
```
50+
51+
The corresponding package manifest entry for the background task is as follows:
52+
53+
```xml
54+
<Extension Category="windows.backgroundTasks" EntryPoint="Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task">
55+
<BackgroundTasks>
56+
<Task Type="general"/>
57+
</BackgroundTasks>
58+
</Extension>
59+
```
60+
61+
A full WinUI 3 background task registration sample can be found on [GitHub](https://github.com/microsoft/WindowsAppSDK-Samples/tree/release/experimental/Samples/BackgroundTask).
62+
63+
## Related content
64+
65+
- [Guidelines for background tasks in UWP apps](/windows/uwp/launch-resume/guidelines-for-background-tasks)
66+
- [Background task migration strategy](../migrate-to-windows-app-sdk/guides/background-task-migration-strategy.md)

hub/apps/windows-app-sdk/migrate-to-windows-app-sdk/api-mapping-table.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ There are differences in the names of namespaces and classes (including UI contr
1818
| UWP | Windows App SDK |
1919
| - | - |
2020
| (**Windows.ApplicationModel.Activation**) [**LaunchActivatedEventArgs**](/uwp/api/windows.applicationmodel.activation.launchactivatedeventargs) class | (**Microsoft.UI.Xaml**) [**LaunchActivatedEventArgs**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.launchactivatedeventargs) class; for example, in **App.OnLaunched**. |
21+
| (**Windows.ApplicationModel.Background**) [**BackgroundTaskBuilder**](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) class | (**Microsoft.Windows.ApplicationModel.Background**) [**BackgroundTaskBuilder**](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder) class. See [Using background tasks in Windows apps](../applifecycle/background-tasks.md). |
2122
| (**Windows.ApplicationModel.Core**) [**CoreApplication.CreateNewView**](/uwp/api/windows.applicationmodel.core.coreapplication.createnewview) method | (**Microsoft.UI.Windowing**) [**AppWindow.Create**](/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindow.create) method |
2223
| (**Windows.ApplicationModel.Core**) [**CoreApplicationViewTitleBar**](/uwp/api/windows.applicationmodel.core.coreapplicationviewtitlebar) class | (**Microsoft.UI.Windowing**) [**AppWindowTitleBar**](/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindowtitlebar) class |
2324
| (**Windows.ApplicationModel.Core**) [**CoreApplicationViewTitleBar.ExtendViewIntoTitleBar**](/uwp/api/windows.applicationmodel.core.coreapplicationviewtitlebar.extendviewintotitlebar) property | (**Microsoft.UI.Windowing**) [**AppWindowTitleBar.ExtendsContentIntoTitleBar**](/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindowtitlebar.extendscontentintotitlebar) property. The platform continues to draw the **Minimize**/**Maximize**/**Close** buttons for you, and reports the occlusion information. |

hub/apps/windows-app-sdk/migrate-to-windows-app-sdk/feature-mapping-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This topic compares major feature areas in the different forms in which they app
1717
| Container | App container:<br/>- security = LowIL<br/>- file system access is brokered<br/>- no registry access | MSIX Container:<br/>- security = MediumIL<br/>- file system access same as user, AppData writes virtualized<br/>- HKCU registry writes virtualized | Moving to a higher integrity level with the Windows App SDK allows your app to have greater functionality. However, be aware of virtualization if you want to expand the capabilities of your migrated application to write to HKCU or AppData. |
1818
| Activation and instancing | Package identity + CoreApplication activation, single-instanced by default | Package identity, Main/WinMain + Windows App SDK activation, multi-instanced by default | Ensure your application can handle multi-instance behavior, or use [**AppInstance**](/windows/windows-app-sdk/api/winrt/microsoft.windows.applifecycle.appinstance) to manage your instances. |
1919
| Lifecycle-managed | Suspend/resume | Power/State notifications | You can use Power/State change notifications to reduce system load. |
20-
| Background tasks | InProc and OOP background tasks | Inproc COM and OOP background tasks | You can continue to use your OOP background tasks. If the app requires communication to your main process, then evaluate your IPC mechanism, as the OOP background task is running in LowIL, and your Windows App SDK main process is running in MediumIL.<br/><br/>Any inproc background tasks need to be migrated to COM background tasks&mdash;see [Create and register a winmain COM background task](/windows/uwp/launch-resume/create-and-register-a-winmain-background-task).<br/><br/>For C# OOP background tasks, see [Author Windows Runtime components with C#/WinRT](../../develop/platform/csharp-winrt/authoring.md) and the [Background task sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/BgTaskComponent). |
20+
| Background tasks | InProc and OOP background tasks | Full trust COM background task implementation | For more info, see [Background task migration strategy](guides/background-task-migration-strategy.md). |
2121
| Windowing | CoreWindow, AppWindow (preview) | HWND, AppWindow v2 | Windowing behavior has significantly changed in Windows App SDK. See [Windowing functionality migration](guides/windowing.md). |
2222
| Messaging | CoreDispatcher and DispatcherQueue | DispatcherQueue, WndProc | [**DispatcherQueue**](/windows/windows-app-sdk/api/winrt/microsoft.ui.dispatching.dispatcherqueue) supports Win32 apps. For additional details on moving from CoreDispatcher to DispatcherQueue see [Threading functionality migration](guides/threading.md). |
2323
| UI Platform| System XAML, WebView, DirectX, and others | WinUI 3, Webview2, DirectX, and others | For more info, see [WinUI migration](guides/winui3.md). |

0 commit comments

Comments
 (0)