2
votes

I'm working on Windows Phone 7 application using a background-agent to update a Live Tile. The problem I've ran into has to do with disabling and re-enabling the background-agent. Users has the ability to disable background-agents for a specific application under settings (Settings - Applications - Background Tasks).

If the Background Agent is disabled, I get the expected behavior of an InvalidOperationException with the message "BNS Error: The action is disabled" if I try to schedule the agent using the following code:

ScheduledActionService.LaunchForTest(PeriodicTaskName, TimeSpan.FromSeconds(20));      

If the user goes back into the settings menu and tick the check box "Turn background tasks back on for this app the next time I open it" I still get the same exception.

So my question is how do I execute a task that has been re-enabled?

The MSDN documentation describes the behavior of disabled tasks, but now how to enable them again.

1
Just a guess, but did the OS automatically turn the background task back on for you and then did your call fail as it was already enabled? - Paul Annetts

1 Answers

0
votes

First of all, you shouldn't call ScheduledActionService.LaunchForTest outside a debugging scenario.

And secondly, there are only 3 reasons a Scheduled Task can be disabled

  1. User disabled it manually
  2. The phone ran out of power, and started using Battery Saver. The task will be re-enabled next time you start your application, when there's enough power on.
  3. Your Task didn't call NotifyComplete(), and as such it was disabled.

In the case of 3. you have to re-create the task. Which usually means the user re-creating the live-tile from your application. Certainly not a recommendation!

So I will suggest you go through your code and ensure that NotifyComplete() is always called, as well as stop using ScheduledActionService.LaunchForTest.