|
| 1 | +--- |
| 2 | +title: Background Task Helper |
| 3 | +author: nmetulev |
| 4 | +description: The Background Task Helper helps users interact with background tasks in an easier manner. |
| 5 | +keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, Background Task Helper |
| 6 | +dev_langs: |
| 7 | + - csharp |
| 8 | + - vb |
| 9 | +--- |
| 10 | + |
| 11 | +# Background Task Helper (Archived) |
| 12 | + |
| 13 | +> [!WARNING] |
| 14 | +> This document has been archived and the component is not available in the current version of the Windows Community Toolkit. |
| 15 | +> |
| 16 | +> While there are no immediate plans to port this component to 8.x, the community is welcome to express interest or contribute to its inclusion. |
| 17 | +> |
| 18 | +> For more information: |
| 19 | +> - [Community Toolkit GitHub repository](https://github.com/CommunityToolkit/Windows) |
| 20 | +> - [Documentation feedback and suggestions](https://github.com/MicrosoftDocs/CommunityToolkit/issues) |
| 21 | +> |
| 22 | +> Original documentation follows below. |
| 23 | +
|
| 24 | +--- |
| 25 | + |
| 26 | +The [Background Task Helper](/dotnet/api/microsoft.toolkit.uwp.helpers.backgroundtaskhelper) helps users interact with background tasks in an easier manner. |
| 27 | + |
| 28 | +> [!div class="nextstepaction"] |
| 29 | +> [Try it in the sample app](uwpct://Helpers?sample=BackgroundTaskHelper) |
| 30 | +
|
| 31 | +## Syntax |
| 32 | + |
| 33 | +```csharp |
| 34 | +using Microsoft.Toolkit.Uwp; |
| 35 | + |
| 36 | +BackgroundTaskRegistration registered = BackgroundTaskHelper.Register(typeof(BackgroundTaskClass), new TimeTrigger(15, true)); |
| 37 | +BackgroundTaskRegistration registered = BackgroundTaskHelper.Register("TaskName", "TaskEntryPoint", new TimeTrigger(15, true)); |
| 38 | +``` |
| 39 | + |
| 40 | +```vb |
| 41 | +Imports Microsoft.Toolkit.Uwp |
| 42 | + |
| 43 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register(GetType(BackgroundTaskClass), New TimeTrigger(15, True)) |
| 44 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register("TaskName", "TaskEntryPoint", New TimeTrigger(15, True)) |
| 45 | +``` |
| 46 | + |
| 47 | +## Methods |
| 48 | + |
| 49 | +| Methods | Return Type | Description | |
| 50 | +| -- | -- | -- | |
| 51 | +| GetBackgroundTask(String) | [IBackgroundTaskRegistration](/uwp/api/Windows.ApplicationModel.Background.IBackgroundTaskRegistration) | Get the registered background task of the given type | |
| 52 | +| GetBackgroundTask(Type) | IBackgroundTaskRegistration | Get the registered background task of the given type | |
| 53 | +| IsBackgroundTaskRegistered(String) | bool | Check if a background task is registered | |
| 54 | +| IsBackgroundTaskRegistered(Type) | bool | Check if a background task is registered | |
| 55 | +| Register(String, IBackgroundTrigger, Boolean, Boolean, IBackgroundCondition[]) | [BackgroundTaskRegistration](/uwp/api/Windows.ApplicationModel.Background.BackgroundTaskRegistration) | Registers under the Single Process Model | |
| 56 | +| Register(Type, IBackgroundTrigger, Boolean, Boolean, IBackgroundCondition[]) | BackgroundTaskRegistration | Register a background task with conditions | |
| 57 | +| Register(String, String, IBackgroundTrigger, Boolean, Boolean, IBackgroundCondition[]) | BackgroundTaskRegistration | Register a background task with conditions | |
| 58 | +| Unregister(String, Boolean) | void | Unregister a background task | |
| 59 | +| Unregister(Type, Boolean) | void | Unregister a background task | |
| 60 | +| Unregister(IBackgroundTaskRegistration, Boolean) | void | Unregister a background task | |
| 61 | + |
| 62 | +## Example |
| 63 | + |
| 64 | +### Using Multi-Process Model |
| 65 | + |
| 66 | +Using MPM (Multi-Process Model) is the classic way of using Background Task. |
| 67 | + |
| 68 | +To make it work, you will need : |
| 69 | + |
| 70 | +* To create Background Tasks in a Windows Runtime Component |
| 71 | +* To register the Background Tasks in the package manifest (appxmanifest file) |
| 72 | + |
| 73 | +Once it is done, you can register your Background Tasks. |
| 74 | + |
| 75 | +```csharp |
| 76 | +// Be sure to include the using at the top of the file: |
| 77 | +using Microsoft.Toolkit.Uwp; |
| 78 | +using Windows.ApplicationModel.Background; |
| 79 | + |
| 80 | +// Register a normal, separate process, background task |
| 81 | +BackgroundTaskRegistration registered = BackgroundTaskHelper.Register("TaskName", "TaskEntryPoint", new TimeTrigger(15, true)); |
| 82 | + |
| 83 | +// This can also be written using the overload of Register with Type parameter. |
| 84 | +BackgroundTaskRegistration registered = BackgroundTaskHelper.Register(typeof(BackgroundTaskClass), new TimeTrigger(15, true)); |
| 85 | + |
| 86 | +// With condition |
| 87 | +BackgroundTaskRegistration registered = |
| 88 | + BackgroundTaskHelper.Register(typeof(BackgroundTaskClass), |
| 89 | + new TimeTrigger(15, true), |
| 90 | + false, true, |
| 91 | + new SystemCondition(SystemConditionType.InternetAvailable)); |
| 92 | + |
| 93 | +// 2 or more conditions |
| 94 | +BackgroundTaskRegistration registered = |
| 95 | + BackgroundTaskHelper.Register(typeof(BackgroundTaskClass), |
| 96 | + new TimeTrigger(15, true), |
| 97 | + false, true, |
| 98 | + new SystemCondition(SystemConditionType.InternetAvailable), |
| 99 | + new SystemCondition(SystemConditionType.UserPresent)); |
| 100 | +``` |
| 101 | + |
| 102 | +```vb |
| 103 | +' Be sure to include the Imports at the top of the file: |
| 104 | +Imports Microsoft.Toolkit.Uwp |
| 105 | +Imports Windows.ApplicationModel.Background |
| 106 | + |
| 107 | +' Register a normal, separate process, background task |
| 108 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register("TaskName", "TaskEntryPoint", New TimeTrigger(15, True)) |
| 109 | + |
| 110 | +' This can also be written using the overload of Register with Type parameter. |
| 111 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register(GetType(BackgroundTaskClass), New TimeTrigger(15, True)) |
| 112 | + |
| 113 | +' With condition |
| 114 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register(GetType(BackgroundTaskClass), |
| 115 | + New TimeTrigger(15, True), |
| 116 | + False, |
| 117 | + True, |
| 118 | + New SystemCondition(SystemConditionType.InternetAvailable)) |
| 119 | + |
| 120 | +' 2 or more conditions |
| 121 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register(GetType(BackgroundTaskClass), |
| 122 | + New TimeTrigger(15, True), |
| 123 | + False, |
| 124 | + True, |
| 125 | + New SystemCondition(SystemConditionType.InternetAvailable), |
| 126 | + New SystemCondition(SystemConditionType.UserPresent)) |
| 127 | +``` |
| 128 | + |
| 129 | +### Using Single-Process Model |
| 130 | + |
| 131 | +Using SPM (Single-Process Model) is the new and simple way of using Background Task. |
| 132 | +It is required to target Anniversary Update (SDK 14393) or later. |
| 133 | + |
| 134 | +Using SPM, you can declare your Background Tasks inside your own project, no need to create a Windows Runtime Component. |
| 135 | +Moreover, it is no longer required to register the Background Tasks in the package manifest (appxmanifest file). |
| 136 | + |
| 137 | +Once you have created the Background Task, you can register it by calling the `Register` method. |
| 138 | + |
| 139 | +```csharp |
| 140 | +// Be sure to include the using at the top of the file: |
| 141 | +using Microsoft.Toolkit.Uwp; |
| 142 | +using Windows.ApplicationModel.Background; |
| 143 | + |
| 144 | +// Register a single process background task (Anniversary Update and later ONLY) |
| 145 | +BackgroundTaskRegistration registered = BackgroundTaskHelper.Register("Name of the Background Task", new TimeTrigger(15, true)); |
| 146 | +``` |
| 147 | + |
| 148 | +```vb |
| 149 | +' Be sure to include the imports at the top of the file: |
| 150 | +Imports Microsoft.Toolkit.Uwp |
| 151 | +Imports Windows.ApplicationModel.Background |
| 152 | + |
| 153 | +' Register a single process background task (Anniversary Update and later ONLY) |
| 154 | +Dim registered As BackgroundTaskRegistration = BackgroundTaskHelper.Register("Name of the Background Task", New TimeTrigger(15, True)) |
| 155 | +``` |
| 156 | + |
| 157 | +The other difference between SPM and MPM is that in SPM, you have to handle your Background Tasks inside the `OnBackgroundActivated` event of `App.xaml.cs` class. |
| 158 | +Here is an example of how to handle Background Tasks in SPM. |
| 159 | + |
| 160 | +```csharp |
| 161 | +// Event fired when a Background Task is activated (in Single Process Model) |
| 162 | +protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args) |
| 163 | +{ |
| 164 | + base.OnBackgroundActivated(args); |
| 165 | + |
| 166 | + var deferral = args.TaskInstance.GetDeferral(); |
| 167 | + |
| 168 | + switch (args.TaskInstance.Task.Name) |
| 169 | + { |
| 170 | + case "Name of the Background Task": |
| 171 | + new TestBackgroundTask().Run(args.TaskInstance); |
| 172 | + break; |
| 173 | + } |
| 174 | + |
| 175 | + deferral.Complete(); |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +```vb |
| 180 | +Protected Overrides Sub OnBackgroundActivated(ByVal args As BackgroundActivatedEventArgs) |
| 181 | + MyBase.OnBackgroundActivated(args) |
| 182 | + |
| 183 | + Dim deferral = args.TaskInstance.GetDeferral() |
| 184 | + |
| 185 | + Select Case args.TaskInstance.Task.Name |
| 186 | + Case "Name of the Background Task" |
| 187 | + New TestBackgroundTask().Run(args.TaskInstance) |
| 188 | + End Select |
| 189 | + |
| 190 | + deferral.Complete() |
| 191 | +End Sub |
| 192 | +``` |
| 193 | + |
| 194 | +## Sample Project |
| 195 | + |
| 196 | +[Background Task Helper](https://github.com/windows-toolkit/WindowsCommunityToolkit/tree/rel/7.1.0/Microsoft.Toolkit.Uwp.SampleApp/SamplePages/BackgroundTaskHelper). You can [see this in action](uwpct://Helpers?sample=BackgroundTaskHelper) in the [Windows Community Toolkit Sample App](https://aka.ms/windowstoolkitapp). |
| 197 | + |
| 198 | +## Requirements |
| 199 | + |
| 200 | +| Device family | Universal, 10.0.16299.0 or higher | |
| 201 | +| --- | --- | |
| 202 | +| Namespace | Microsoft.Toolkit.Uwp | |
| 203 | +| NuGet package | [Microsoft.Toolkit.Uwp](https://www.nuget.org/packages/Microsoft.Toolkit.Uwp/) | |
| 204 | + |
| 205 | +## API |
| 206 | + |
| 207 | +* [Background Task source code](https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/rel/7.1.0/Microsoft.Toolkit.Uwp/Helpers/BackgroundTaskHelper.cs) |
0 commit comments