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

[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 obtain 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 Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks property. Use a while loop to go through this list and look for the name of the background task.

For example, the background task sample uses the following code to check whether the servicing-complete background task is already registered before trying to register it:

//
// Check whether the servicing-complete background task is already registered.
//
var iter = Windows.ApplicationModel.Background.BackgroundTaskRegistration.allTasks.first();
var hascur = iter.hasCurrent;
while (hascur) {
    var cur = iter.current.value;
    if (cur.name === BackgroundTaskSample.servicingCompleteTaskName) {
        BackgroundTaskSample.updateBackgroundTaskStatus(BackgroundTaskSample.servicingCompleteTaskName, true);
        return;
    }
    hascur = iter.moveNext();
}

Step 2:

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.
// 
function FindTask(taskName) {

    var taskRegistered = false;

    var background = Windows.ApplicationModel.Background;
    var iter = background.BackgroundTaskRegistration.allTasks.first();
    var hascur = iter.hasCurrent;

    while (hascur) {
        var cur = iter.current.value;

        if (cur.name === taskName) {

            // 
            // The task is registered, return it.
            // 

            return cur;
        }

        hascur = iter.moveNext();
    }

    // 
    // The task was not found. Return null.
    // 
    return null;
}

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