WinJS scheduling

A new scheduler API in Windows 8.1 lets you set the priority of tasks and manage jobs. This helps you write HTML5 apps that use system resources more efficiently and provide a more responsive experience to your users.

Writing apps that are always responsive can be challenging. You can combine multiple, complex UI elements, including ListView controls, SemanticZoom controls, and custom controls that all compete for system resources. Many elements require both immediate foreground work—such as animation— and additional background work.

With Windows 8 and the Windows Library for JavaScript 1.0, there was no single integrated scheduler. A low-priority background task could preempt foreground work and cause unresponsive or stuttering app behavior. Because work was scheduled on different queues, there was no easy way for you to coordinate between the queues to get the prioritization you want.

Windows 8.1 and the Windows Library for JavaScript 2.0 introduces the Scheduler, an object/namespace that consolidates all work queues into a single, universal queue. This new queue has a priority-based scheduling policy that supports fast and fluid apps and a more unified developer experience. You can easily schedule tasks and assign priorities so that the right things get done at the right time.

The Scheduler coordinates with the work performed by the Windows Library for JavaScript and the prioritized, asynchronous work performed by the Windows Runtime. Using the scheduler, you can get your app work scheduled at the correct priority in relation to all other work in the system.

Scheduling jobs

To schedule work, you use the schedule method to create a job. You pass this method a function that performs the task that you want to accomplish. You can also specify a priority for the job. The schedule method returns an object that implements the IJob interface, and you can use this object to pause, resume, and cancel the job that you scheduled.

Here's an example that schedules two simple jobs at normal priority.


// Schedule some work.
var job1 = S.schedule(function () {
    window.output("Running job1");
}, S.Priority.normal);
window.output("Scheduled job1");

// Schedule more work.
var job2 = S.schedule(function () {
    window.output("Running job2");
}, S.Priority.normal);
window.output("Scheduled job2");

For the full code, see the Scheduler sample.

For additional control, your work function can accept an IJobInfo object. You can use this object to determine whether the job should yield, schedule a second job to run after the current job finishes, and specify a function to run if the job yields control.

Managing jobs

The preceding section mentioned that you can use the IJob object returned by the schedule method to pause, cancel, and resume jobs. You can also use the Scheduler object's createOwnerToken method to obtain an IOwnerToken and use it to control any IJob objects it owns.

This example creates several IOwnerToken objects and uses them to set the owner property of several IJob objects. It then uses the IOwnerToken to cancel multiple jobs at once.


var ownerObject1 = S.createOwnerToken();
var ownerObject2 = S.createOwnerToken();
var ownerObject3 = S.createOwnerToken();

// Schedule some work.
var job1 = S.schedule(function () {
    window.output("Running job1 with owner1");
}, S.Priority.normal);
job1.owner = ownerObject1;
window.output("Scheduled job1 with owner1");

// Schedule some work.
var job2 = S.schedule(function () {
    window.output("Running job2 with owner2");
}, S.Priority.normal);
job2.owner = ownerObject2;
window.output("Scheduled job2 with owner2");

// Schedule some work.
var job3 = S.schedule(function () {
    window.output("Running job3 with owner1");
}, S.Priority.normal);
job3.owner = ownerObject1;
window.output("Scheduled job3 with owner1");

// Schedule some work.
var job4 = S.schedule(function () {
    window.output("Running job4 with owner3");
}, S.Priority.normal);
job4.owner = ownerObject3;
window.output("Scheduled job4 with owner3");

window.output("Canceling jobs with owner1");
ownerObject1.cancelAll();

For the full code, see the Scheduler sample.

For additional examples, including examples that show yielding and draining, see the Scheduler sample.

 

 

Show:
© 2014 Microsoft