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:
- Creating and registering background tasks written in C++, C#, or JavaScript.
- Creating a background task that is triggered by a system event.
- Requesting the user's permission to add the app to the lock screen.
- Creating a background task that is triggered by a time trigger.
- Adding a condition that constrains the background task to run only when the condition is in effect.
- Reporting background task progress and completion to the app.
- Using a deferral object to include asynchronous code in your background task.
- Handling the cancellation of a background task, and ensuring the task is cancelled when required conditions are no longer met.
- Initializing background task progress and completion handlers when the app is launched.
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.BackgroundOperating system requirements
| Client | |
|---|---|
| Server | |
| Phone |
Build the sample
- Start Microsoft Visual Studio 2013 Update 2 and select File > Open > Project/Solution.
- 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.
- Follow the steps for the version of the sample you want:
-
To build the Windows version of the sample:
- Select BackgroundTask.Windows in Solution Explorer.
- Press Ctrl+Shift+B, or use Build > Build Solution, or use Build > Build BackgroundTask.Windows.
-
To build the Windows Phone version of the sample:
- Select BackgroundTask.WindowsPhone in Solution Explorer.
- Press Ctrl+Shift+B or use Build > Build Solution or use Build > Build BackgroundTask.WindowsPhone.
-
Run the sample
Deploying and running the sample
-
To deploy and run the Windows version of the sample:
- Right-click BackgroundTask.Windows in Solution Explorer and select Set as StartUp Project.
- To debug the sample and then run it, press F5 or use Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.
-
To deploy and run the Windows Phone version of the sample:
- Right-click BackgroundTask.WindowsPhone in Solution Explorer and select Set as StartUp Project.
- To debug the sample and then run it, press F5 or use Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.
Triggering background tasks
To trigger the background tasks associated with the TimeZoneChange event:
- Change date and time settings.
- Click Change time zone...
- Select a time zone that has a UTC offset different from the currently selected time zone.
- 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:
- Launch the BackgroundTaskSample app for the first time.
- Register the TimeTrigger event.
- Accept the request to add the BackgroundTaskSample app to the lock screen.
Add the BackgroundTaskSample app to the lock screen manually:
- From the Start screen, go to Settings > Customize your lock screen.
- Choose the BackgroundTaskSample app for the lock screen.
- 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:
- Quickstart: Create and register a background task
- How to respond to system events with background tasks
- How to set conditions for running a background task
- How to handle a cancelled background task
- How to monitor background task progress and completion
- How to run a background task on a timer
- How to use maintenance triggers
- How to declare background tasks in the application manifest
- Guidelines and checklists for background tasks
- How to debug a background task