Windows Phone supports three types of background agents: ScheduledTaskAgent
, AudioPlayerAgent
, and AudioStreamingAgent
(see Figure 1).
FIGURE 1 BackgroundAgent
types.
ScheduledTaskAgent
is an abstract class, which you subclass to provide your own logic to
be performed whenever the OS chooses to invoke your agent. When
invoked, the OS passes the registered task object to the task agent,
which allows you to determine what kind of activity is to be performed.
ScheduledTaskAgents
handle two kinds of tasks: periodic and resource intensive, which are represented by the PeriodicTask
and ResourceIntensiveTask
classes, respectively.
Note
An app may have only one periodic task and/or
one resource intensive task. If an attempt is made to register more
than one of either type, an InvalidOperationException
is raised.
Scheduled tasks are registered in the same manner as ScheduledNotification
objects—with the ScheduledActionService
.
At an indeterminate time after registration, the operating system
instantiates your task agent and passes it the scheduled task (see Figure 2).
The agent performs any processing it needs to do in the background, and
then signals that it has either completed its work or that it is unable
to complete.
FIGURE 2 Periodic tasks are registered with the ScheduledActionService
. The OS invokes the ScheduledTaskAgent
, passing it the PeriodicTask
object.
It is important to recognize the distinction
between the roles of tasks and task agents. Tasks contain information
that is passed to the task agent. Their purpose is to allow the ScheduledActionService
to determine how and when to run the task agent. The task agent
performs the background activity, and that is where you can place your
code to be executed in the background.
PeriodicTask
and ResourceIntensiveTask
are analogous to the ScheduledNotification
classes: Alarm
and Reminder
. ScheduledTask
and ScheduledNotification
both derive from ScheduledAction
, which includes properties for the name and expiration date of the task (see Figure 3).
FIGURE 3 ScheduledTask
is derived from ScheduledAction
, the same base class for scheduled notifications.
The following sections examine the two ScheduledTask
types, PeriodicTask
and ResourceIntensiveTask
, in greater detail.
Periodic Tasks
A periodic task is used to perform short
operations and is allocated a 15-second window to complete its work.
Periodic tasks run, at most, every 30 minutes. Typical scenarios for
periodic tasks include performing small incremental data
synchronization with a cloud service, using geographic location to
track the location of the device, or polling a social network cloud
service for new messages or status updates.
Note
When retrieving the geographic location of the device within a scheduled task (via a GeoCoordinateWatcher
),
a cached location value is used instead of real-time data. This helps
to minimize the power consumption of background agents. It does,
however, also introduce a lag for determining the location of the
device, which may be problematic in some scenarios.
Periodic Task Invocation
There are no guarantees that a periodic task
will ever run. To minimize resources, the phone OS launches all
periodic tasks for all apps on the device at approximately the same
time. This can mean that if a large number of periodic tasks are
registered, not all will be invoked. Moreover, Battery Saver mode is an
option that the user can enable on the device to indicate that battery
life should be prioritized. If this mode is enabled, periodic agents
may not run, even if the task interval has elapsed.
There is also a limit on the total number of
periodic agents that can be scheduled on a device. This limit varies
from device to device, but it can be as low as six. If this limit is
approached, the user is warned that multiple background agents are
running, and that it may cause faster battery consumption.
Resource Intensive Tasks
Resource intensive tasks (also known as on idle
tasks) are allocated a substantially longer execution time than
periodic tasks. Resource intensive tasks, however, run only when the
phone meets a minimum set of requirements relating to processor
activity, power source, and network connection type (see Table 1). This task is typically used to synchronize large amounts of data to the phone during periods of inactivity.
Resource intensive tasks typically run for a
maximum of 10 minutes. The runtime is cut short, however, if one or
more of the requirements that determine eligibility to launch are no
longer met.
TABLE 1. Run Eligibility Requirements for Resource Intensive Tasks
Caution
Due to the hefty requirements for resource
intensive tasks, it is possible that a resource intensive task may
never be launched. Be mindful of this when designing your app, and
allow your foreground app to adapt to the absence of any processing
that should have taken place in the background but did not.