This sample shows you how to create and register background tasks using the Windows Runtime background task API.

Note  This sample was created using one of the universal app templates available in Visual Studio. It shows how its solution is structured so it can run on both Windows 8.1 and Windows Phone 8.1. For more info about how to build apps that target Windows and Windows Phone with Visual Studio, see Build apps that target Windows and Windows Phone 8.1 by using Visual Studio.

A background task is triggered by a system or time event and can be constrained by one or more conditions. When a background task is triggered, its associated handler runs and performs the work of the background task. A background task can run even when the app that registered the background task is suspended.

This sample demonstrates the following:

To obtain an evaluation copy of Windows 8.1, go to Windows 8.1.

To obtain an evaluation copy of Microsoft Visual Studio 2013, go to Visual Studio 2013.

Note  For Windows 8 app samples, download the Windows 8 app samples pack. The samples in the Windows 8 app samples pack will build and run only on Microsoft Visual Studio 2012.

Related topics

Displaying tiles on the lock screen
Launching, resuming, and multitasking
Managing background tasks
Supporting your app with background tasks
Windows 8 app samples
API reference
Windows.ApplicationModel.Background (Windows Store apps using C#/VB/C++ and XAML)
Windows.UI.WebUI.WebUIBackgroundTaskInstance (Windows Store apps using JavaScript and HTML)

Related technologies

Windows.ApplicationModel.Background

Operating system requirements

Client
Windows 8.1
Server
Windows Server 2012 R2
Phone
Windows Phone 8.1

Build the sample

  1. Start Microsoft Visual Studio 2013 Update 2 and select File > Open > Project/Solution.
  2. Go to the directory to which you unzipped the sample. Then go to the subdirectory named for the sample and double-click the Visual Studio 2013 Update 2 Solution (.sln) file.
  3. Follow the steps for the version of the sample you want:
    • To build the Windows version of the sample:

      1. Select BackgroundTask.Windows in Solution Explorer.
      2. Press Ctrl+Shift+B, or use Build > Build Solution, or use Build > Build BackgroundTask.Windows.
    • To build the Windows Phone version of the sample:

      1. Select BackgroundTask.WindowsPhone in Solution Explorer.
      2. Press Ctrl+Shift+B or use Build > Build Solution or use Build > Build BackgroundTask.WindowsPhone.

Run the sample

Deploying and running the sample

Triggering background tasks

To trigger the background tasks associated with the TimeZoneChange event:

  1. Change date and time settings.
  2. Click Change time zone...
  3. Select a time zone that has a UTC offset different from the currently selected time zone.
  4. Click OK.

Background tasks associated with the TimeTrigger event will only fire if the app is currently on the lock screen. There are two ways to accomplish this.

Accept the initial request to add the BackgroundTaskSample app to the lock screen:

  1. Launch the BackgroundTaskSample app for the first time.
  2. Register the TimeTrigger event.
  3. Accept the request to add the BackgroundTaskSample app to the lock screen.

Add the BackgroundTaskSample app to the lock screen manually:

  1. From the Start screen, go to Settings > Customize your lock screen.
  2. Choose the BackgroundTaskSample app for the lock screen.
  3. Launch the BackgroundTaskSample app and register the TimeTrigger event.

Note  The minimum delay for creating TimeTrigger events is 15 minutes. The first timer event, however, might not occur until 15 minutes after it is expected to expire (30 minutes after the app registers the event).

Read more

See the following topics for step-by-step information about using background tasks: