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.

Background Tasks and Lock Screen Apps<br />

At the end of the introduction to this chapter, I described how everything we’ve talked about so far<br />

helps to “create an environment that is alive with activity while those apps are often not actually<br />

running or are allowed to run just a little bit.” It’s that last phrase—being allowed to run just a little bit<br />

through background tasks—that is our last topic for this chapter. And as described earlier as well,<br />

background tasks are related to the lock screen because the apps that are allowed to work on the lock<br />

screen must also employ certain background tasks.<br />

Let me reiterate here that we’ve already seen a number of scenarios in which the user can experience<br />

app-related activity without the app having to run. Periodic tile updates, push notifications, scheduled<br />

toasts, and even sharing data through the Share contract all provide for activity when an app is<br />

suspended or not running. Apps can also configure background data transfers to occur while the app<br />

isn’t running, as we’ll see in Chapter 14, “Networking.” And background audio provides for that specific<br />

class of apps that need to continue running to maintain VoIP sessions, audio playback, online meetings,<br />

and so forth.<br />

What’s left in the story are those little pieces of app code that Windows can run in response to<br />

specific triggers. Triggers in some cases can be further refined with optional conditions so that the<br />

background task runs only when it really needs to. This is all to minimize the amount of background<br />

activity that can drain a device’s battery. Some types of triggers, in fact, require that the user has placed<br />

the app on the lock screen to specifically limit the number of apps that can respond to those triggers.<br />

Furthermore, Windows also limits the amount of CPU time that background tasks can consume:<br />

• Lock screen background tasks Two seconds of cumulative CPU time per 15 minutes.<br />

• Other background tasks One second of cumulative CPU time every two hours.<br />

Consumption of network bandwidth is also limited on battery power. What that limit is, exactly, I<br />

cannot say, because the system analyzes energy usage more so than bytes transferred. A means of<br />

estimating the limit can be found on Supporting your app with background tasks. (While we’re at it, you<br />

might also be interested in Guidelines and checklists for background tasks, the Introduction to<br />

background tasks whitepaper, and Being productive in the background – background tasks on the<br />

Windows Blog.)<br />

“Whoa!” you’re probably saying, “Is Windows 8 really that restrictive?”<br />

The short answer is yes, because Windows not only wants to save battery power for the foreground<br />

app with which a user is engaged, but also wants to make sure the foreground app delivers the best<br />

user experience. This means it isn’t having to complete with background apps that would, if allowed,<br />

take up as many resources as they possibly can. (Developers of background services will always find a<br />

justification for hogging up resources!)<br />

It’s likely that you’ve experienced situations like this directly, where you’ve started an app but it takes<br />

for-EV-er to start up because some other dark and mysterious services is chewing on the hard drive,<br />

615

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

Saved successfully!

Ooh no, something went wrong!