Siva Rajagopal
Siva Rajagopal

Reputation: 71

how to run two background tasks periodically in wp8 apps?

In my Windows Phone 8 apps, I have to tasks(LiveTiles and ToastNotifications). I want to run those tasks as a periodical background tasks. The Toast Notification task to be run once per day and my LiveTiles task to be run in every 10 minutes. While adding second Periodic task, It showing an error(BNS Error: The maximum number of ScheduledActions of this type have already been added). Any one let me know the answer if you have a solution. Here I attached the code.

App.xaml.cs:

    var LiveTilesName = "LiveTiles";
        var ToastNotificationsName = "ToastNotifications";

        PeriodicTask LiveTilesPeriodicTask = ScheduledActionService.Find(LiveTilesName) as PeriodicTask;
        PeriodicTask ToastNotificationPeriodicTask = ScheduledActionService.Find(ToastNotificationsName) as PeriodicTask;

        if (LiveTilesPeriodicTask != null)
            ScheduledActionService.Remove(LiveTilesName);

        if (ToastNotificationPeriodicTask != null)
            ScheduledActionService.Remove(ToastNotificationsName);

        LiveTilesPeriodicTask = new PeriodicTask(LiveTilesName) { Description = "Update Live Tiles." };
        ToastNotificationPeriodicTask = new PeriodicTask(ToastNotificationsName) { Description = "Toast Notifications" };

        try
        {
            ScheduledActionService.Add(LiveTilesPeriodicTask);
            ScheduledActionService.LaunchForTest(LiveTilesName, TimeSpan.FromSeconds(10));

            ScheduledActionService.Add(ToastNotificationPeriodicTask);
            ScheduledActionService.LaunchForTest(ToastNotificationsName, TimeSpan.FromSeconds(10));

        }
        catch (InvalidOperationException e) { }

SchedulerAgentTask Code:

    protected override void OnInvoke(ScheduledTask task)
    {
        //ScheduledActionService.LaunchForTest("ToastNotifications", TimeSpan.FromSeconds(30));
        if (task.Name == "ToastNotifications")
        {
            SendNotifications();                                
        }
        else if(task.Name == "LiveTiles")
        {

            UpdateTiles();                
            ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(30));
        }

        else{}

        NotifyComplete();
    }

Upvotes: 0

Views: 1813

Answers (2)

Siva Rajagopal
Siva Rajagopal

Reputation: 71

I found the solution for my question.Hereby I added code. In my App.xaml.cs file I have a Method called StartAgentTask(),

    private void StartAgentTask()
    {
        var LiveTilesName = "LiveTiles";
        var ToastNotificationsName = "ToastNotifications";

        PeriodicTask LiveTilesPeriodicTask = ScheduledActionService.Find(LiveTilesName) as PeriodicTask;
        ResourceIntensiveTask ToastNotificationResourceIntensiveTask = ScheduledActionService.Find(ToastNotificationsName) as ResourceIntensiveTask;
        if (LiveTilesPeriodicTask != null)
            ScheduledActionService.Remove(LiveTilesName);

        if (ToastNotificationResourceIntensiveTask != null)
            ScheduledActionService.Remove(ToastNotificationsName);

        LiveTilesPeriodicTask = new PeriodicTask(LiveTilesName) { Description = "Update Live Tiles." };
        ToastNotificationResourceIntensiveTask = new ResourceIntensiveTask(ToastNotificationsName) { Description = "Toast Notifications" };

        try
        {
            ScheduledActionService.Add(LiveTilesPeriodicTask);
            ScheduledActionService.LaunchForTest(LiveTilesName, TimeSpan.FromSeconds(10));

            ScheduledActionService.Add(ToastNotificationResourceIntensiveTask);

            double seconds;
            if (DateTime.Now.TimeOfDay <= new TimeSpan(8, 30, 00))
            {
                seconds = 30600 - DateTime.Now.TimeOfDay.TotalSeconds;
                //seconds = 38100 - DateTime.Now.TimeOfDay.TotalSeconds;
            }
            else
            {
                seconds = 117000 - DateTime.Now.TimeOfDay.TotalSeconds;
                //seconds = 124500 - DateTime.Now.TimeOfDay.TotalSeconds;
            }

            ScheduledActionService.LaunchForTest(ToastNotificationsName, TimeSpan.FromSeconds(seconds));

        }
        catch (InvalidOperationException e) { }

    }

In My Scheduler Agent Class I have a method called OnInvoke(ScheduledTask Task),

    protected override void OnInvoke(ScheduledTask Task)
    {
        //ScheduledActionService.LaunchForTest("ToastNotifications", TimeSpan.FromSeconds(30));
        if (Task.Name == "ToastNotifications")
        {                             
            SendNotifications(); // To Call the SendNotification method and It'l be send the notification to the user at the specified time when the application is not running
            double seconds;
            if (DateTime.Now.TimeOfDay <= new TimeSpan(8, 30, 00))
            {
                seconds = 30600 - DateTime.Now.TimeOfDay.TotalSeconds;
                //seconds = 38100 - DateTime.Now.TimeOfDay.TotalSeconds;
            }
            else
            {
                seconds = 117000 - DateTime.Now.TimeOfDay.TotalSeconds;
                //seconds = 124500 - DateTime.Now.TimeOfDay.TotalSeconds;
            }

            ScheduledActionService.LaunchForTest(Task.Name, TimeSpan.FromSeconds(seconds));              
        }
        else if(Task.Name == "LiveTiles")
        {
            UpdateTiles(); // To Cal the UpdateTiles method and  It'l update the current tile.               
            ScheduledActionService.LaunchForTest(Task.Name, TimeSpan.FromSeconds(30));
        }

        else{}

        NotifyComplete();
    }

I have to call the first method from my Application_Launching() method. So that my first task to be scheduled to perform every 30 seconds, and my second task to be scheduled to perform @8.30 AM every day(from my sample).

Upvotes: 0

Matt Lacey
Matt Lacey

Reputation: 65564

As you've discovered it's only possible to have a single background task for an application.

It is however possible to have that task perform multiple functions. If you want one to only happen once a day just keep track of if it's been run that day already.

You should also note that it's not possible to run a task every 10 minutes.
Scheduled tasks are run approximately every 30 minutes (usually +/- 10 minutes) and you cannot control the times that they run. They are scheduled by the OS to optimise battery consumption.

If you want to, potentially, update your tiles every 10 minutes you'll need to do this via push notifications.

Upvotes: 3

Related Questions