23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

pounding the network, flaring up the CPU, and so forth. I, for one, have dug through Task Manager and<br />

the Resource Monitor to figure out—and kill off—whatever process is pouring molasses on my system,<br />

let come what will. This is the kind of user experience that Windows 8 is trying to avoid.<br />

“OK,” you say (assuming that I’ve actually convinced you to some small degree), “does that mean<br />

that there isn’t any way to do some background work like indexing data, creating picture thumbnails,<br />

processing video, and so on?”<br />

Actually, there are ways to do this. For one, when an app is in the foreground, it can do however<br />

much it wants of all these things because it’s ultimately responsible for its own user experience. (An app<br />

written in JavaScript can, for such purposes, employ web workers to move such work off the UI thread,<br />

as well as delegate tasks to WinRT components that do their work on other threads and return results<br />

asynchronously. We’ll look at this in Chapter 16.)<br />

Second, when a device is on AC power instead of battery, Windows allows apps to run background<br />

tasks in response to maintenance triggers on 15-minute or longer intervals (whatever is appropriate for<br />

the app). These are still limited in the total amount of CPU time they can consume, but tasks that don’t<br />

involve UI—which background tasks are not allowed—can burn through a few billion instructions in one<br />

or two seconds on a gigahertz CPU!<br />

What we have in this whole story, then, are three distinct classes of background tasks and their<br />

associated triggers:<br />

• Tasks for maintenance triggers that run on AC power only<br />

• Tasks for potentially conditioned system triggers that run on AC or battery and don’t require<br />

being on the lock screen<br />

• Tasks for those privileged apps that the user had added to the lock screen<br />

We’ll look at each of these in detail in the sections that follow, but first there are a few aspects that<br />

are shared by them all: declaring background tasks in the manifest, the general process of building the<br />

task with the WinRT API, and conditions.<br />

Background Tasks in the Manifest<br />

All background tasks for an app are declared in the manifest, where each declaration indicates the type<br />

of task as well as the code to execute for that task, as shown in Figure 13-18. We’ve seen this section of<br />

the manifest before in Chapter 10 where we checked Audio for a background audio app. As for the<br />

other options, System Event is used for the first two classes of background tasks in the list above, and<br />

Control Channel, Timer, and Push Notification are specifically for tasks reserved for lock screen apps.<br />

616

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!