programming4us
 
Technology
 

Windows Phone 8 : Background Agents (part 2) - Resource-Intensive Agent

8/17/2013 11:08:29 AM

2. Resource-Intensive Agent

Like a periodic agent, a resource-intensive agent is a repeatable task that performs some discrete process in the background. Unlike the periodic agent, though, the resource-intensive agent is not meant to be executed very often. In fact, there are a strict set of rules as to when resource-intensive agents are executed. These agents are meant to be able to run for a longer period (up to 10 minutes) and consume more resources (for instance, network, and memory). But to allow your agent to be executed, the operating system must only allow it when doing so is not detrimental to the phone itself. To that end, the criteria for executing a resource-intensive agent include

• On external power

• When using a noncellular network (for example, Wi-Fi or plugged into a PC)

• When the minimum battery level is 90%

• When the device must be screen-locked

• When no phone call is active

These rules should imply the fact that resource-intensive agents are meant for syncing large amounts of data or processing that will be accomplished occasionally. These agents are often executed only once a day, at most. Deciding whether to use an agent (or deciding to use a periodic agent or resource-intensive agent) can increase the overall usefulness of your application.

If the criteria for executing a resource-intensive agent change while the agent is being executed (for instance, a phone call comes in or the phone is removed from external power), the resource-intensive agent will immediately be aborted to allow the user full access to the phone.


Warning

Resource-intensive agents have so many requirements to enable them to be executed that some users will never be able to execute these agents. For example, users who don’t dock their phone with a PC or use Wi-Fi will never execute a resource-intensive agent.


Both resource-intensive and periodic agents are scheduled agents. So whether you’re adding a periodic or a resource-intensive agent (or even if you need both), you will have a single Scheduled Task Agent project .

Registering a resource-intensive agent is virtually identical to registering a periodic agent:

// A unique name for your task. It is used to
// locate it in from the service.
var taskName = "IntensiveTask";

// If the task exists
var oldTask = ScheduledActionService.Find(taskName);
if (oldTask != null)
{
  ScheduledActionService.Remove(taskName);
}

// Create the Task
ResourceIntensiveTask task = new ResourceIntensiveTask(taskName);

// Description is required
task.Description = "This does a lot of work.";

// Add it to the service to execute
ScheduledActionService.Add(task);

The only real difference is that the class you create is an instance of ResourceIntensiveTask (instead of PeriodicTask). By specifying that the new task is a resource-intensive task, the operating system will know to execute the agent only when the phone is ready for a resource-intensive operation (for example, it is in a state as defined by the previously mentioned limitations).

You can test resource-intensive agents in the same way as well, by calling the ScheduledActionService’s LaunchForTest method:

var taskName = "IntensiveTask";

ScheduledActionService.LaunchForTest(taskName,
  TimeSpan.FromMilliseconds(10));

You might want to have both a periodic and a resource-intensive task registered for the phone. The problem is that an application can have only a single background agent (the Scheduled Task Agent project) associated with an application. You can register one (and only one) of each type of scheduled task. This means you can have a periodic task and a resource-intensive task for your application, but because there is only one agent, you must discriminate which type of task is being called in your agent by checking the task type, like so:

public class ScheduledAgent : ScheduledTaskAgent
{
  protected override void OnInvoke(ScheduledTask task)
  {
    if (task is ResourceIntensiveTask)
    {
      DoHeavyResources();
    }
    else
    {
      DoPeriodic();
    }

    NotifyComplete();
  }

  // ...
}

In this way, you can determine the type of task the OnInvoke is meant to execute. You could discriminate by name as well, but because you can have only one of each type, testing by object type is just as effective.


 
Others
 
- Windows Phone 8 : Background Agents (part 1) - Periodic Agent
- Active Directory 2008 : Configuring Computer Accounts - Supporting Computer Objects and Accounts
- Active Directory 2008 : Automating the Creation of Computer Objects
- Administration of Microsoft Lync Server 2010 : Troubleshooting (part 2) - Lync Server Logging Tool
- Administration of Microsoft Lync Server 2010 : Troubleshooting (part 1)
- Administration of Microsoft Lync Server 2010 : Configuring Quality of Service
- Administration of Microsoft Lync Server 2010 : Management Tasks
- Windows 8 : Maintaining Data Access and Availability - Managing Offline Files (part 3) - Configuring Disk Usage Limits for Offline Files
- Windows 8 : Maintaining Data Access and Availability - Managing Offline Files (part 2) - Managing Offline File Synchronization
- Windows 8 : Maintaining Data Access and Availability - Managing Offline Files (part 1) - Making Files or Folders Available Offline
 
Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
 
REVIEW
 
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- Sigma 24mm f/1.4 DG HSM Art

- Canon EF11-24mm f/4L USM

- Creative Sound Blaster Roar 2

- Alienware 17 - Dell's Alienware laptops

- Smartwatch : Wellograph

- Xiaomi Redmi 2
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
 
Top 10
 
- How To Unlock An Account From Active Directory (Windows Server 2008)
- How To Install A deviantArt Theme In Windows 7
- How To Install Windows Server 2012 On VirtualBox
- How To Fix Skype High CPU And Memory Usage In Windows 8
- Add 270 Additional Cleaning Options To CCleaner With CCEnhancer
- FPPT Provides More Than 2000 Free And Attractive PowerPoint Templates
- Setup Free Media Server To Stream Videos To DLNA Compatible TV, Xbox 360 & PS3 (Play Station 3)
- How To Install Android Market & Google Apps On Kindle Fire
- How To Make Ubuntu Look Like Windows 7
- How To Add A New Account in MS Outlook 2013