HTML and CSS Reference
In-Depth Information
//Tiles.sendTileUpdate();
//Tiles.sendBadgeUpdate();
Location.startTracking();
});
}
function performResume(e) {
Location.startTracking();
}
function performSuspend(e) {
var promise = Location.stopTracking();
if (promise) {
var deferral = e.suspendingOperation.getDeferral();
promise.then(function () {
deferral.complete();
});
}
}
})();
The changes for the activated and resuming events are simple: in both cases I want to start
my background task, so I just have to call the
Location.startTracking
method. The interesting
part of this listing, and the reason that I included the example in this chapter, is how I handle
the
suspending
event.
■
Notice I have commented out the lines that apply the tile and badge updates. This is so that
the example app will run in the simulator. I have also disabled the event handlers in the
tiles.js
file.
Tip
My problem is that any background task that is active when Windows suspends my app
automatically carries on when the app is resumed. Depending on where in the request cycle
the task was suspended, I can expect to see an error (for example, trying to read data from a net-
work request that timed out during suspension) or stale data (because my task was just about to
update the DOM when the app was suspended).
To help work around these problems, the
suspending
event defines a property called
sus-
pendingOperation
that returns a
Windows.ApplicationModel.SuspendingOperation
object.
Calling this object's
getDeferral
method asks Windows to give your app a little more time to
prepare for suspension. When you have finished winding up your background tasks, you call the
complete
method on the object that the
getDeferral
method returned, signaling to Windows
that your app is now ready to be suspended.
Asking for a deferral grants an extra five seconds to prepare for suspension. This may not
sound like a lot, but it is pretty generous given that Window may be under a lot of pressure to get
your app out of the way to make system resources available.
■
In the Consumer Preview, Windows will terminate a Metro app that doesn't call
the
complete
method on the deferral object within the five-second allowance. I imagine that this
will change before the final release, but it is worth paying close attention to.
Caution
Search WWH ::
Custom Search