Skip to content

Commit ced0356

Browse files
committed
Add background tasks for winappsdk
1 parent 44ded41 commit ced0356

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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.topic: article
5-
ms.date: 10/07/2021
4+
ms.date: 01/05/2025
5+
ms.topic: concept-article
6+
keywords: windows 11, winui, background task, app service, connected devices, remote systems, app lifecycle
67
ms.localizationpriority: medium
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.
79
---
810

9-
# App lifecycle and system services
11+
# App lifecycle, background tasks, and system services
1012

1113
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.
1214

@@ -20,8 +22,9 @@ The [Windows App SDK](../windows-app-sdk/index.md) provides the following featur
2022
|---------|-------------|
2123
| [App lifecycle](../windows-app-sdk/applifecycle/applifecycle.md) | Get an overview of managing the lifecycle of your app. |
2224
| [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. |
2326
| [Rich activation](../windows-app-sdk/applifecycle/applifecycle-rich-activation.md) | Receive information about different kinds activations for your app. |
24-
| [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. |
2528
| [Restart](../windows-app-sdk/applifecycle/applifecycle-restart.md) | Programmatically restart your application and set restart options after app termination. |
2629

2730
## Windows OS features

hub/apps/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ items:
4242
href: windows-app-sdk/applifecycle/applifecycle.md
4343
- name: App instancing
4444
href: windows-app-sdk/applifecycle/applifecycle-instancing.md
45+
- name: Background tasks
46+
href: windows-app-sdk/applifecycle/background-tasks.md
4547
- name: Create a single-instanced app
4648
href: windows-app-sdk/applifecycle/applifecycle-single-instance.md
4749
- name: Rich activation
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Using background tasks in Windows apps
3+
description: This article provides an overview of using background tasks and describes how to create a new background task in a WinUI app.
4+
ms.date: 01/05/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 app.
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 previous [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 to be registered with Windows Runtime (WinRT) components that are to be 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 have to do a workaround of including the WinRT components in the project. This API avoids this workaround so WinUI and other desktop applications that use Windows App SDK can register the full trust COM components with the background tasks directly.
20+
21+
## Register a background task
22+
23+
The following code registers a background task for full trust COM component. You'll also need to set the **WindowsAppSDKBackgroundTask** property to **true** in the project configuration. Additionally, in the manifest file, the **EntryPoint** for **BackgroundTask** must be set to **Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task**.
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+
## Related content
62+
63+
[Guidelines for background tasks in UWP apps](/windows/uwp/launch-resume/guidelines-for-background-tasks)

0 commit comments

Comments
 (0)