共用方式為


How to get a list of pending background tasks (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Learn how to get a list of background tasks currently registered by your app. This list includes background tasks registered by the app during previous sessions in the foreground, so that you can check before potentially re-registering a background task multiple times. For more info see How to register a background task.

What you need to know

Technologies

Prerequisites

Instructions

Step 1:

The list of background tasks currently registered by the application is kept in the BackgroundTaskRegistration.AllTasks property.

The following code shows how to iterate on the AllTasks property:

foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
    // TODO: Perform operations relevant to registered background tasks, such as registering event handlers.
}
auto iter = BackgroundTaskRegistration::AllTasks->First();
auto hascur = iter->HasCurrent;

while (hascur)
{
    auto cur = iter->Current->Value;
    
    // TODO: Perform operations relevant to registered background tasks, such as registering event handlers.
    
    hascur = iter->MoveNext();
}

Step 2:

Getting the list of background tasks currently registered by the application isn't very helpful on its own - you'll want your app to do something useful with that information. See the example snippet below, and also see How to monitor background task progress and completion for more information.

For example, the background task sample uses the following code to check whether the SampleBackgroundTask (without the condition) is already registered before trying to register it:

/// <summary>
/// Registers a background task for the servicing-complete system event.
/// This event occurs when the application is updated.
/// </summary>
public static void RegisterServicingCompleteTask()
{
    //
    // Check whether the servicing-complete background task is already registered.
    //
    foreach (var cur in BackgroundTaskRegistration.AllTasks)
    {
        if (cur.Value.Name == ServicingCompleteTaskName)
        {
            //
            // The task is already registered.
            //
            UpdateBackgroundTaskStatus(ServicingCompleteTaskName, true);
            return;
        }
    }

    //
    // The servicing-complete background task is not already registered.
    //
    RegisterBackgroundTask(ServicingCompleteTaskEntryPoint,
                            ServicingCompleteTaskName,
                            new SystemTrigger(SystemTriggerType.ServicingComplete, false),
                            null);
}
//
// Registers a background task for the servicing-complete system event. This event occurs when the application is updated.
//
void BackgroundTaskSample::RegisterServicingCompleteBackgroundTask()
{
    //
    // Check whether the servicing-complete background task is already registered.
    //
    auto iter = BackgroundTaskRegistration::AllTasks->First();
    auto hascur = iter->HasCurrent;
    while (hascur)
    {
        auto cur = iter->Current->Value;

        if (cur->Name == ServicingCompleteTaskName)
        {
            //
            // The task is already registered.
            //
            return;
        }

        hascur = iter->MoveNext();
    }

    //
    // The servicing-complete background task is not already registered.
    //
    RegisterBackgroundTask(ServicingCompleteTaskEntryPoint,
                           ServicingCompleteTaskName,
                           ref new SystemTrigger(SystemTriggerType::ServicingComplete, false),
                           nullptr);
}

Step 3:

An app that registers background tasks is likely to look up registrations in more than one situation. You can make this task easier by creating a function to find a background task based on its name. If this function doesn't find the task it returns null so that, for example, your app can determine whether to register the task.

The following function can be used in your app:

// 
// Check for a registration of the named background task. If one exists,
// return it.
// 
public static BackgroundTaskRegistration FindTask(string taskName)
{
    foreach (var cur in BackgroundTaskRegistration.AllTasks)
    {
        if (cur.Value.Name == taskName)
        {
            // 
            // The task is registered, return it.
            // 
                    
            return (BackgroundTaskRegistration)cur.Value;
        }
    }
    

    // 
    // The task was not found. Return null.
    // 

    return null;
}
// 
// Check for a registration of the named background task. If one exists,
// return it.
// 
BackgroundTaskRegistration ^ Class1::FindTask(Platform::String ^ taskName)
{
    auto iter   = BackgroundTaskRegistration::AllTasks->First();
    auto hascur = iter->HasCurrent;
    
    while (hascur)
    {
        auto cur = iter->Current->Value;
        
        if(cur->Name == taskName)
        {
            // 
            // The task is registered, return it.
            // 
                    
            return (BackgroundTaskRegistration ^)(cur);
        }
        
        hascur = iter->MoveNext();
    }
       
 
    // 
    // The task was not found. Return a null pointer.
    // 

    return nullptr;
}

Quickstart: Create and register a background task

How to register a background task

How to handle a cancelled background task

How to monitor background task progress and completion

How to declare background tasks in the application manifest

How to debug a background task

Guidelines and checklists for background tasks