Rich Hopkins
Rich Hopkins

Reputation: 1891

Scheduled Task appears Twice

I have failed submission (can't even get past upload verification) because my background agent creates two instances, but I can't figure out why.

Here is the method, in app.xaml.cs (I have also tried locating it in the MainPageVM.cs and MainPage.xaml.cs with the same results)

private void RegisterBackgroundTask()
{
    var taskName = "xxUpdater";
    var oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
    if (oldTask != null)
    {
        ScheduledActionService.Remove(taskName);
    }
    PeriodicTask task = new PeriodicTask(taskName);
    task.Description = Strings.xxBackgroundTaskDescription;
    oldTask = ScheduledActionService.Find(taskName) as PeriodicTask;
    if (oldTask == null)
    {
        ScheduledActionService.Add(task);                 
    }
    //ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(60));
}

Here is the call, in Application_Launching:

private void Application_Launching(object sender, LaunchingEventArgs e)
{
      { RegisterBackgroundTask(); }
}

The first time the app runs, I back out, look, and there is one task. Then I run again, stepping through, and the remove call turns off the task (instead of removing it). When it gets to the add call, not only does it turn the original task on, but it adds another. From that point on, when I back out and restart the app, the remove turns ONE instance off, and leaves the other, and the Add call turns it back on. Never is oldTask null except after rebuild or uninstall.

The if(oldTask ==old) wasn't initially there, I added it hoping that it would prevent the creation of the second instance. Sometimes, when I uninstall, there is still a background task that is turned off, and I have to turn the phone off and back on to get it to go away.

Upvotes: 2

Views: 455

Answers (1)

Jimmy Engtröm
Jimmy Engtröm

Reputation: 2008

I have the same problem with the project I'm working on.
For some reason I had the backgroundtask defined twice in my WMAppManifest.xml
Check you WMAppManifest.xml and see if you also have it defined twice.

Upvotes: 10

Related Questions