Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

Applies to Windows and Windows Phone

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.
}

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);
}

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;
}

Related topics

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

 

 

Show:
© 2014 Microsoft