重要的应用程序接口(API)
BackgroundTaskRegistrationGroup 类
后台任务现在可以在组中注册,你可以将其视为逻辑命名空间。 这种隔离有助于确保应用的不同组件或不同的库不会相互干扰后台任务注册。
当应用和其使用的框架(或库)使用同名来注册后台任务时,应用可能会无意中取消框架的后台任务注册。 应用作者还可以意外删除框架和库后台任务注册,因为它们可以使用 BackgroundTaskRegistration.AllTask 取消注册所有已注册的后台任务。 使用组功能,可以隔离后台任务注册,来避免发生这种情况。
组的特征
- 组可以通过全球唯一标识符 (GUID) 来唯一标识。 它们还可以有一个相关的易读名称字符串,在调试时更易于阅读。
- 可以在一个组中注册多个后台任务。
- 在组中注册的后台任务不会显示在 BackgroundTaskRegistration.AllTasks中。 因此,当前使用 BackgroundTaskRegistration.AllTasks 注销其任务的应用不会无意中注销在组中注册的后台任务。 请参阅下面的组
注销后台任务,了解如何注销已注册为组一部分的所有后台触发器。 - 每个后台任务注册都有一个组属性,用于确定它与之关联的组。
- 向组注册 In-Process 后台任务将导致激活通过 BackgroundTaskRegistrationGroup.BackgroundActivated 事件,而不是 Application.OnBackgroundActivated。
在组中注册后台任务
下面演示如何将后台任务(在此示例中由时区更改触发)注册为组的一部分。
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();
}
}
取消注册组中的后台任务
下面演示如何注销注册为组一部分的后台任务。 由于组中注册的后台任务不会出现在 BackgroundTaskRegistration.AllTasks中,因此必须循环访问组,找到注册到每个组的后台任务,然后注销它们。
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
}
}
注册持久事件
在使用进程内后台任务的后台任务注册组时,后台任务的激活会被定向到该组的事件,而不是应用程序或 CoreApplication 对象上的事件。 这使应用内的多个组件能够处理激活,而不是将所有激活代码路径放在 Application 对象中。 下面演示如何注册组的后台激活事件。 首先检查 BackgroundTaskRegistration.GetTaskGroup 以确定该组是否已注册。 如果没有,请使用 ID 和友好名称创建新组。 然后为组的 BackgroundActivated 事件注册一个事件处理程序。
void RegisterPersistentEvent()
{
var group = BackgroundTaskRegistration.GetTaskGroup(groupId);
if (group == null)
{
group = new BackgroundTaskRegistrationGroup(groupId, groupFriendlyName);
}
group.BackgroundActivated += MyEventHandler;
}