繁体   English   中英

如何在wp8应用程序中定期运行两个后台任务?

[英]how to run two background tasks periodically in wp8 apps?

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

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代码:

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

如您所知,一个应用程序只能有一个后台任务。

但是,可以使该任务执行多种功能。 如果您希望一天只发生一次,请跟踪它是否已在当天运行。

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

如果您可能需要每隔10分钟更新一次地图块,则需要通过推送通知进行此操作。

我找到了问题的解决方案,在此添加了代码。 在我的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) { }

    }

在我的Scheduler Agent类中,我有一个称为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 AM(来自我的示例)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM