簡體   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