共用方式為


群組背景任務註冊

重要的應用程式介面

BackgroundTaskRegistrationGroup 類別

背景工作現在可以在群組中註冊,您可以將其視為邏輯命名空間。 這種隔離有助於確保應用程式的不同元件或不同的程式庫不會干擾彼此的背景工作登錄。

當應用程式和其使用的架構(或程式庫)註冊具有相同名稱的背景任務時,應用程式可能會不小心移除該架構的背景任務註冊。 應用程式作者也可能不小心移除架構和連結庫背景工作註冊,因為它們可以使用 BackgroundTaskRegistration.AllTask 來取消註冊所有已註冊的背景工作。 使用群組時,您可以將背景工作註冊進行隔離管理,因此不會發生這種情況。

群組的功能

  • 群組可由 GUID 唯一識別。 它們也可以有相關聯的易記名稱字串,在偵錯時更容易閱讀。
  • 您可以在群組中註冊多個背景工作。
  • 在群組中註冊的背景工作不會出現在 backgroundTaskRegistration.AllTasks。 因此,目前使用 BackgroundTaskRegistration.AllTasks 取消註冊其工作的應用程式不會不小心取消註冊群組中註冊的背景工作。 請查看下方的 取消註冊群組中的背景任務,以瞭解如何取消註冊作為群組一部分的所有背景觸發程式。
  • 每個背景工作註冊都會有一個 Group 屬性,以判斷它與哪個群組相關聯。
  • 向群組註冊 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
    }
}

註冊永續性事件

當與進程內的背景工作搭配使用背景工作註冊群組時,背景啟用會導向群組的事件,而非導向 Application 或 CoreApplication 物件上的事件。 這可讓應用程式內的多個元件處理啟用,而不是將所有啟用程式代碼路徑放在 Application 物件中。 以下說明如何註冊群組的背景啟動事件。 請先檢查 backgroundTaskRegistration.GetTaskGroup,以判斷群組是否已註冊。 如果沒有,請使用您的識別碼和易記名稱建立新的群組。 然後在群組上註冊 BackgroundActivated 事件的處理程式。

void RegisterPersistentEvent()
{
    var group = BackgroundTaskRegistration.GetTaskGroup(groupId);
    if (group == null)
    {
        group = new BackgroundTaskRegistrationGroup(groupId, groupFriendlyName);
    }

    group.BackgroundActivated += MyEventHandler;
}