Monday, February 20, 2012

[Solution] "BNS error. The maximum number of SheduledActions of this type have already been added."

Symptoms

ScheduledActionService.Add() methods throws an InvalidOperationException exception with the following text: "BNS error. The maximum number of ScheduledActions of this type have already been added.".

Explanation

WP7 has a hardcoded limit of 1 (one) PeriodicTask per application.

So, if you already had added a PeriodicTask, you can't add more, and ScheduledActionService.Add() method throws an InvalidOperationException exception with the following text: "BNS error. The maximum number of SheduledActions of this type have already been added.".

It can happen for example when you change the name of your PeriodicTask (passed as a parameter in constructor).

Solution

Have exactly 1 (one) PeriodicTask per application.

Easiest way is to remove existing PeriodicTask before adding a new one:

    try
    {
        foreach (PeriodicTask oldTask in ScheduledActionService.GetActions<PeriodicTask>())
        {
            ScheduledActionService.Remove(oldTask.Name);
        }

        PeriodicTask newTask = new PeriodicTask("UpdateTile");
        newTask.Description = "Updates the tile on the start screen.";
        newTask.ExpirationTime = DateTime.Now.AddDays(14);

        ScheduledActionService.Add(newTask);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

1 comment:

  1. thanks! I couldn't figure out why I was getting that exception but yet my PeriodicTask was STILL running!

    ReplyDelete