0

在我的 Windows Phone 8 应用程序中,我必须执行任务(LiveTiles 和 ToastNotifications)。我想将这些任务作为定期后台任务运行。Toast 通知任务每天运行一次,我的 LiveTiles 任务每 10 分钟运行一次。添加第二个定期任务时,它显示错误(BNS 错误:已添加此类型的最大 ScheduledActions 数量)。如果您有解决方案,任何人都可以告诉我答案。在这里我附上了代码。

应用程序.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) { }

SchedulerAgent任务代码:

    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();
    }
4

2 回答 2

3

正如您所发现的,应用程序只能有一个后台任务。

然而,可以让该任务执行多种功能。如果您希望每天只发生一次,只需跟踪它是否已经在当天运行。

您还应该注意,不可能每 10 分钟运行一次任务。
计划任务大约每 30 分钟运行一次(通常为 +/- 10 分钟),您无法控制它们的运行时间。它们由操作系统安排以优化电池消耗。

如果您希望每 10 分钟更新一次磁贴,则需要通过推送通知执行此操作。

于 2013-09-24T11:38:30.607 回答
0

我找到了我的问题的解决方案。因此我添加了代码。在我的 App.xaml.cs 文件中,我有一个名为 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) { }

    }

在我的调度程序代理类中,我有一个名为 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();
    }

我必须从我的 Application_Launching() 方法中调用第一个方法。这样我的第一个任务被安排为每 30 秒执行一次,而我的第二个任务被安排为每天上午 8.30 执行(来自我的示例)。

于 2013-09-26T05:49:12.277 回答