Skip to content

Commit 258f35b

Browse files
committed
Add background task migration guide and mappings
1 parent 01c627a commit 258f35b

File tree

6 files changed

+110
-5
lines changed

6 files changed

+110
-5
lines changed

hub/apps/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ items:
11091109
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/dwritecore.md
11101110
- name: MRT to MRTCore migration
11111111
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/mrtcore.md
1112+
- name: Background task migration strategy
1113+
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/background-task-migration-strategy.md
11121114
- name: Threading functionality migration
11131115
href: windows-app-sdk/migrate-to-windows-app-sdk/guides/threading.md
11141116
- name: Case study 1—PhotoLab (C#)

hub/apps/windows-app-sdk/applifecycle/background-tasks.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ ms.localizationpriority: medium
1010

1111
# Using background tasks in Windows apps
1212

13-
This article provides an overview of using background tasks and describes how to create a new background task in a WinUI app.
13+
This article provides an overview of using background tasks and describes how to create a new background task in a WinUI 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).
1414

1515
## BackgroundTaskBuilder in the Windows App SDK
1616

1717
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.
1818

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.
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 Windows App SDK API avoids this workaround so WinUI and other desktop applications that use Windows App SDK can register the full trust COM components directly with background tasks.
2020

2121
## Register a background task
2222

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**.
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.
2424

2525
The C++ code to create and register a background task is as follows:
2626

@@ -60,4 +60,5 @@ The corresponding package manifest entry for the background task is as follows:
6060

6161
## Related content
6262

63-
[Guidelines for background tasks in UWP apps](/windows/uwp/launch-resume/guidelines-for-background-tasks)
63+
- [Guidelines for background tasks in UWP apps](/windows/uwp/launch-resume/guidelines-for-background-tasks)
64+
- [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 | Inproc COM and OOP background tasks | 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). |
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Background task migration strategy
3+
description: Considerations and strategies for approaching the migration process, and how to migrate your UWP background tasks to use the Windows App SDK background task APIs.
4+
ms.topic: concept-article
5+
ms.date: 02/26/2025
6+
keywords: Windows, App, SDK, migrate, migrating, migration, port, porting
7+
ms.localizationpriority: medium
8+
# customer intent: As a Windows developer, I want to learn about the considerations and strategies for migrating my UWP background tasks to use the Windows App SDK background task APIs.
9+
---
10+
11+
# Background task migration strategy
12+
13+
Background tasks are used heavily in UWP apps for performing jobs when a particular trigger is invoked on the machine. As these don’t require any service to be running listening for the triggers, it's very power efficient. So, while migrating UWP apps to WinUI and other desktop apps that use Windows App SDK, developers may require support for implementing background tasks on the platform.
14+
15+
Background tasks offered in the UWP app model can either be out-of-proc or in-proc. Below is the migration strategy for each of these types when moving to the Windows App SDK [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder) APIs.
16+
17+
## Out-of-proc background tasks
18+
19+
In the case of out-of-proc background tasks in UWP, background tasks will be written in a Windows Runtime (WinRT) component and the component would be given to [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) as a [TaskEntryPoint](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder.taskentrypoint?view=winrt-26100) during registration. While migrating to Windows App SDK, developers can keep this as-is and package the WinRT component together with the desktop project. In this case, the broker infrastructure will be launching `backgroundtaskhost` process when the trigger is invoked, and the WinRT component background task would be executed in the process. In this approach, the background task would be running in a Low Integrity Level (IL) process (`backgroundtaskhost.exe`) while the main desktop project would be running in a Medium IL process.
20+
21+
If the application needs both to run in a medium IL process, then full trust COM component needs to be used for background task. In that scenario, developers must go with [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder) API for successful registration of the full trust COM component. Note that the [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) API from the **Windows.ApplicationModel.Background** namespace will throw exceptions while registering of some of the triggers.
22+
23+
A background task registration sample can be found [here](https://github.com/microsoft/WindowsAppSDK-Samples/tree/release/experimental/Samples/BackgroundTask).
24+
25+
More details on the implementation are available [here](/windows/uwp/launch-resume/create-and-register-a-winmain-background-task). The only required change would be to replace the WinRT [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) API with the Windows App SDK API [Microsoft.Windows.ApplicationModel.Background.BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder).
26+
27+
## In-proc background tasks
28+
29+
In case of in-proc background tasks in UWP, background task routines are implemented in the [OnBackgroundActivated](/uwp/api/windows.ui.xaml.application.onbackgroundactivated) callback which runs as part of the foreground process. This won't be possible in a WinUI application as the **OnBackgroundActivated** callbacks aren't available. The application needs to move the background task implementations to full trust COM tasks as described above and define the COM server in the package manifest to handle the COM activation of the task. When the trigger occurs, COM activation will happen on the corresponding [COM Coclass](/windows/uwp/cpp-and-winrt-apis/author-coclasses#implement-the-coclass-and-class-factory) registered for the trigger.
30+
31+
A background task registration sample can be found [here](https://github.com/microsoft/WindowsAppSDK-Samples/tree/release/experimental/Samples/BackgroundTask).
32+
33+
More details on the implementation are available [here](/windows/uwp/launch-resume/create-and-register-a-winmain-background-task). Only change would be to replace the WinRT [BackgroundTaskBuilder](/uwp/api/windows.applicationmodel.background.backgroundtaskbuilder) API with the Windows App SDK APIs in [Microsoft.Windows.ApplicationModel.Background.BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder).
34+
35+
## Windows App SDK BackgroundTaskBuilder API
36+
37+
This new API is created to support the full trust COM background task implementations in WinUI and other desktop applications using Windows App SDK, as the WinRT API throws exceptions during registration except for a few triggers. The following code shows how to register background task using Windows App SDK [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder) APIs:
38+
39+
```cpp
40+
//Using Windows App SDK API for BackgroundTaskBuilder
41+
winrt::Microsoft::Windows::ApplicationModel::Background::BackgroundTaskBuilder builder;
42+
SystemTrigger trigger = SystemTrigger(SystemTriggerType::TimeZoneChange, false);
43+
auto backgroundTrigger = trigger.as<IBackgroundTrigger>();
44+
builder.SetTrigger(backgroundTrigger);
45+
builder.AddCondition(SystemCondition(SystemConditionType::InternetAvailable));
46+
builder.SetTaskEntryPointClsid(classGuid);
47+
builder.Register();
48+
```
49+
50+
Here is the equivalent C# code:
51+
52+
```csharp
53+
// Using Windows App SDK API for BackgroundTaskBuilder
54+
var builder = new Microsoft.Windows.ApplicationModel.Background.BackgroundTaskBuilder();
55+
var trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
56+
builder.SetTrigger(trigger);
57+
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
58+
builder.SetTaskEntryPointClsid(classGuid);
59+
builder.Register();
60+
```
61+
62+
For using this API, developers would need to add a specific tag as below in their project file:
63+
64+
``` xml
65+
<WindowsAppSDKBackgroundTask>true</WindowsAppSDKBackgroundTask>
66+
```
67+
68+
Also in the manifest file **EntryPoint** for **BackgroundTask** is set as **Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task**:
69+
70+
``` xml
71+
<Extension Category="windows.backgroundTasks" EntryPoint="Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task">
72+
<BackgroundTasks>
73+
<Task Type="general"/>
74+
</BackgroundTasks>
75+
</Extension>
76+
```
77+
78+
For C# applications, an **ActivatableClass** registration may also need to be added manually as below:
79+
80+
```xml
81+
<Extension Category="windows.activatableClass.inProcessServer">
82+
<InProcessServer>
83+
<Path>Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.dll</Path>
84+
<ActivatableClass ActivatableClassId="Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.Task" ThreadingModel="both"/>
85+
</InProcessServer>
86+
</Extension>
87+
```
88+
89+
## Leveraging TaskScheduler for background task migration
90+
91+
[Task Scheduler](/windows/win32/api/_taskschd/) helps in achieving 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/).
92+
93+
## ApplicationTrigger use in Windows App SDK applications
94+
95+
[ApplicationTrigger](/uwp/api/windows.applicationmodel.background.applicationtrigger) is supported in UWP applications due to the lifetime management scenario where the application process can be suspended. This scenario does not occur for WinUI and other Windows App SDK desktop applications, so this trigger is not supported in WinUI applications. All logic related to **ApplicationTrigger** will need to be rewritten to execute by launching another process or by running in a thread pool thread. For more information, see [CreateThread](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread) and [CreateProcess](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa).
96+
97+
## Related content
98+
99+
- [Using background tasks in Windows apps](/windows/apps/windows-app-sdk/applifecycle/background-tasks)
100+
- [BackgroundTaskBuilder](/windows/windows-app-sdk/api/winrt/microsoft.windows.applicationmodel.background.backgroundtaskbuilder)

hub/apps/windows-app-sdk/migrate-to-windows-app-sdk/guides/feature-area-guides-ovw.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ A collection of migration guidance topics, each focusing on a specific feature a
2323
| [DirectWrite to DWriteCore migration](dwritecore.md) | DWriteCore is the [Windows App SDK](../../index.md) implementation of [DirectWrite](/windows/win32/directwrite/direct-write-portal). |
2424
| [MRT to MRT Core migration](mrtcore.md) | This topic contains guidance for migrating from UWP's [Resource Management System](/windows/uwp/app-resources/resource-management-system) (also known as MRT) to the Windows App SDK's MRT Core. |
2525
| [Threading functionality migration](threading.md) | This topic shows how to migrate your threading code. |
26+
| [Background task migration strategy](background-task-migration-strategy.md) | This topic contains considerations and strategies for approaching the migration process, and how to migrate your UWP background tasks to use the Windows App SDK background task APIs. |
2627

2728
## See Also
2829

0 commit comments

Comments
 (0)