Events
19 Nov, 23 - 21 Nov, 23
Gain the competitive edge you need with powerful AI and Cloud solutions by attending Microsoft Ignite online.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Important APIs
BackgroundTaskRegistrationGroup class
Background tasks can now be registered in a group, which you can think of as a logical namespace. This isolation helps ensure that different components of an app, or different libraries, don’t interfere with each other’s background task registration.
When an app and the framework (or library) it uses registers a background task with the same name, the app could inadvertently remove the framework's background task registrations. App authors could also accidentally remove framework and library background task registrations because they could unregister all registered background tasks by using BackgroundTaskRegistration.AllTasks. With groups, you can isolate your background task registrations so this doesn't happen.
The following shows how to register a background task (triggered by a time zone change, in this example) as part of a group.
private const string groupFriendlyName = "myGroup";
private const string groupId = "3F2504E0-4F89-41D3-9A0C-0305E82C3301";
private const string myTaskName = "My Background Trigger";
public static void RegisterBackgroundTaskInGroup()
{
BackgroundTaskRegistrationGroup group = BackgroundTaskRegistration.GetTaskGroup(groupId);
bool isTaskRegistered = false;
// See if this task already belongs to a group
if (group != null)
{
foreach (var taskKeyValue in group.AllTasks)
{
if (taskKeyValue.Value.Name == myTaskName)
{
isTaskRegistered = true;
break;
}
}
}
// If the background task is not in a group, register it
if (!isTaskRegistered)
{
if (group == null)
{
group = new BackgroundTaskRegistrationGroup(groupId, groupFriendlyName);
}
var builder = new BackgroundTaskBuilder();
builder.Name = myTaskName;
builder.TaskGroup = group; // we specify the group, here
builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
// Because builder.TaskEntryPoint is not specified, OnBackgroundActivated() will be raised when the background task is triggered
BackgroundTaskRegistration task = builder.Register();
}
}
The following shows how to unregister background tasks that were registered as part of a group. Because background tasks registered in a group don't appear in BackgroundTaskRegistration.AllTasks, you must iterate through the groups, find the background tasks registered to each group, and unregister them.
private static void UnRegisterAllTasks()
{
// Unregister tasks that are part of a group
foreach (var groupKeyValue in BackgroundTaskRegistration.AllTaskGroups)
{
foreach (var groupedTask in groupKeyValue.Value.AllTasks)
{
groupedTask.Value.Unregister(true); // passing true to cancel currently running instances of this background task
}
}
// Unregister tasks that aren't part of a group
foreach(var taskKeyValue in BackgroundTaskRegistration.AllTasks)
{
taskKeyValue.Value.Unregister(true); // passing true to cancel currently running instances of this background task
}
}
When using Background Task Registration Groups with in-process background tasks, the background activations are directed towards the group's event instead of the one on the Application or CoreApplication object. This enables multiple components within your app to handle the activation rather than place all activation code paths in the Application object. The following shows how to register for the group's background activated event. First check BackgroundTaskRegistration.GetTaskGroup to determine if the group has already been registered. If not then create a new group with your id and friendly name. Then register an event handler to the BackgroundActivated event on the group.
void RegisterPersistentEvent()
{
var group = BackgroundTaskRegistration.GetTaskGroup(groupId);
if (group == null)
{
group = new BackgroundTaskRegistrationGroup(groupId, groupFriendlyName);
}
group.BackgroundActivated += MyEventHandler;
}
Events
19 Nov, 23 - 21 Nov, 23
Gain the competitive edge you need with powerful AI and Cloud solutions by attending Microsoft Ignite online.
Register now