23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The Background task sample provides a few examples of these triggers. In Scenario 1,<br />

js/sample-background-task-with-condition.js, we can see the use of timeZoneChange along with the<br />

userPresent condition (where BackgroundTaskSample is again a helper object in global.js):<br />

BackgroundTaskSample.registerBackgroundTask(BackgroundTaskSample.sampleBackgroundTaskEntryPoint,<br />

BackgroundTaskSample.sampleBackgroundTaskWithConditionName,<br />

new Windows.ApplicationModel.Background.SystemTrigger(<br />

Windows.ApplicationModel.Background.SystemTriggerType.timeZoneChange, false),<br />

new Windows.ApplicationModel.Background.SystemCondition(<br />

Windows.ApplicationModel.Background.SystemConditionType.userPresent));<br />

This is clearly a case where I’d use another variable to not type the Windows.Application-<br />

Model.Background namespace out every time, but at least you can’t make a mistake in reading this code!<br />

In any case, the same sample, in Scenario 4 and js/global.js, also shows use of the servicing-Complete<br />

trigger within a helper function registerServicingCompleteTask, which also checks if the task is already<br />

registered:<br />

"registerServicingCompleteTask": function () {<br />

// Check whether the servicing-complete background task is already registered.<br />

var iter =<br />

Windows.ApplicationModel.Background.BackgroundTaskRegistration.allTasks.first();<br />

var hascur = iter.hasCurrent;<br />

while (hascur) {<br />

var cur = iter.current.value;<br />

if (cur.name === BackgroundTaskSample.servicingCompleteTaskName) {<br />

BackgroundTaskSample.updateBackgroundTaskStatus(<br />

BackgroundTaskSample.servicingCompleteTaskName, true);<br />

return;<br />

}<br />

hascur = iter.moveNext();<br />

}<br />

},<br />

// The servicing-complete background task is not already registered.<br />

BackgroundTaskSample.registerBackgroundTask(<br />

BackgroundTaskSample.servicingCompleteTaskEntryPoint,<br />

BackgroundTaskSample.servicingCompleteTaskName,<br />

new Windows.ApplicationModel.Background.SystemTrigger(<br />

Windows.ApplicationModel.Background.SystemTriggerType.servicingComplete, false),<br />

null);<br />

In the sample, the tasks associated with these triggers are implemented in C#, within a WinRT<br />

component found in the Tasks project of the sample’s solution. I won’t show the code here because<br />

we’ll be looking at the general structure of WinRT components in Chapter 16. What it does<br />

demonstrate, though, is that you can use a mixed-language approach for background tasks. In these<br />

cases, the Entry Point field for the tasks in the manifest point to the C# class/method that implements<br />

the background task, such as Tasks.ServicingComplete. If you go to the Background task sample page,<br />

you can also download the C# and C++ versions of the sample to see even more structural variants.<br />

623

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

Saved successfully!

Ooh no, something went wrong!