2

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.

Jonas Follesø
  • 6,441
  • 7
  • 41
  • 54
  • 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 Sep 05 '11 at 10:46

1 Answers1

0

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.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • I know about the LaunchForTest limitation (if not I would have gotten a different Exception than the "BNS Error: The action is disabled". As I describe in the question this is a case 1 - the user has disabled it, and then later enabled it. So when I enumerate all the tasks I can find it, but the "IsEnabled" flag is set to true (even if the user has clicked to enable the task again). – Jonas Follesø Sep 05 '11 at 13:08
  • So, what's your *actual* problem then? – Claus Jørgensen Sep 05 '11 at 13:18
  • Hehe - why IsEnabled is set to false even if the user enables it again under Settings menu. – Jonas Follesø Sep 05 '11 at 13:20
  • Okay. Also, why does it matter? I got tasks generating live tiles, and I've never had to check for IsEnabled. – Claus Jørgensen Sep 05 '11 at 13:22
  • Well, it's more that I wan't to understand the intended behavior if a Task has been disabled (and later enabled) as the only way I got it to work now is to remove the task and re-create it (as the existing one won't start up again even if it has been re-enabled in settings) – Jonas Follesø Sep 05 '11 at 13:33
  • Yes, re-create it by attempting to first remove it, and then attempt to create it. This is how I do it: https://github.com/Windcape/DMI-Byvejr/blob/master/DMI.Service/TileGenerator.cs#L161 – Claus Jørgensen Sep 05 '11 at 13:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3175/discussion-between-claus-jorgensen-and-jonas-folleso) – Claus Jørgensen Sep 05 '11 at 13:40