Background Tasks in Windows 8.1
- Threads Versus Background Tasks
- Uploads and Downloads
- Audio
- Lock Screen Tasks
- Timer
- Conditions
- Raw Push Notifications
- Control Channel
- System Events
- Summary
Windows Store apps are designed to remain alive and connected even when they are not in the foreground. Users expect a consistent experience. They are accustomed to hearing a weather alert when a storm is on the way or for their email client to automatically fetch messages when they are in the range of Wi-Fi. News apps will download fresh content so it is available for offline reading, and an alarm app must trigger at the right time to achieve its purpose.
The Windows Runtime provides a special API for handling background tasks. In this article, you’ll learn about the various ways Windows 8.1 empowers apps to provide a user-friendly experience across a range of devices and device conditions. From lock screen notifications to music controls, WinRT provides the APIs you need to deliver a delightful user experience. This article references several example apps that demonstrate background tasks. You can download the free sample source code from http://winrtexamples.codeplex.com.
Threads Versus Background Tasks
It is important to understand the difference between thread management and background tasks. In traditional desktop apps, you have several options to spawn tasks that run in the background. You could use the thread pool for shorter tasks that have an affinity to the main application. A common practice is to use a thread to perform long-running work while keeping the UI thread free to process user input, then return data to the UI thread to present. For scenarios that require work to be performed regardless of whether the main app has been launched, a popular solution is to use Windows services.
The Windows Runtime simplifies this process. Although there is support for the thread pool in Windows Store apps, threads that are spawned by the app are tied to the application lifecycle. This means those threads will be frozen when the app is suspended and terminated when the app is closed. WinRT background tasks, on the other hand, are easier to install and execute than traditional Windows services, are managed by the operating system, and will run even after your app has been closed.