共用方式為


Running code in the background

[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]

You can run a task in the background to perform work when your main app is not running.

Although Windows Store apps can't run while in the background (that is, when they are suspended or terminated), there are specific circumstances when you can run code when your app isn't in the foreground. These include:

  • Playing audio
  • Uploading or download files
  • When a portion of the app, known as a task, is configured to run when certain conditions are met, including a timer event and notifications.
  • Updating a live tile with fresh information,
  • Updating an item on the lock screen.

Playing audio in the background

Playing audio is a special type of background task. See How to play audio in the background.

Uploading and downloading files

Longer file transfers can continue after your app has been moved to the background by using the Windows.Networking.BackgroundTransfer APIs. See Transferring data in the background (Windows Store apps using C#/VB/C++ and XAML) or Transferring data in the background (Windows Store apps using JavaScript and HTML).

Background tasks

Background tasks are triggered by specific events, combined with optional conditions. For example, your task may launched every 15 minutes (the event) but only if there is internet access available (the condition). See Supporting your app with background tasks (Windows Store apps using C#/VB/C++ and XAML) or Supporting your app with background tasks (Windows Store apps using JavaScript and HTML) for info and sample code.

Updating a live tile

Windows 8 uses live tiles (see Tiles, badges, and notifications) to provide up-to-date information. See Quickstart: Update a live tile from a background task for details on refreshing a tile even when your app isn't running.

Updating a lock screen item

The Windows 8lock screen can display up to seven app status icons (such as email, instant messaging and calendar apps). An app that registers to display on the lock screen can refresh its information every fifteen minutes. For example code, see Quickstart: Showing tile and badge updates on the lock screen (Windows Store apps).

Topics for iOS devs

Resources for iOS devs

Windows 8 controls for iOS devs

Windows 8 cookbook for iOS devs

Background task topics

Guidelines for background tasks (Windows Store apps)

How to debug a background task (Windows Store apps)