2013-09-24 50 views
0

在我的Windows Phone 8應用程序中,我必須執行任務(LiveTiles和ToastNotifications)。我想將這些任務作爲定期的後臺任務來運行。 Toast Notification任務每天運行一次,LiveTiles任務每10分鐘運行一次。在添加第二個定期任務時,它顯示一個錯誤(BNS錯誤:此類型的ScheduledAction的最大數量已被添加)。如果您有解決方案,任何人都可以讓我知道答案。在這裏我附上了代碼。如何在wp8應用程序中定期運行兩個後臺任務?

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

請勿使用LaunchForTest發佈版本應用程序:http://blog.mjfnet.com/2013/01/10/windows-phone-dont-call-launchfortest-in-release/ – MikimotoH

回答

3

正如你已經發現它是隻可能有一個應用程序的單個後臺任務。

但是可以讓該任務執行多個功能。如果你想要一個人每天只發生一次,只要跟蹤它是否已經在當天運行。

您還應該注意,每10分鐘運行一次任務是不可能的。
計劃任務大約每30分鐘運行一次(通常爲+/- 10分鐘),您無法控制它們運行的​​時間。他們計劃由操作系統優化電池消耗。

如果您希望每隔10分鐘更新您的圖塊,您需要通過推送通知來更新圖塊。

+0

謝謝Matt Lacey,我發現解決方案。我很快就會上傳我的工作代碼。 –

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) { } 

    } 

在我的Scheduler代理類我有一個名爲OnInvoke(ScheduledTask任務)方法,

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(從我的示例中)。

+1

請注意''ScheduledActionService.LaunchForTest'只能在DEBUG條件下使用。它不能用於發佈代碼中,不能用於提交給商店的應用程序。 –

+0

好的,馬特,那麼解決方案是什麼?如果你有任何問題,請深入解釋。 –

+0

你基本上做了你所做的事情,但需要依靠代理調用的自動調度。 'LaunchForTest'只是一個輔助方法輔助測試。這樣你就不必等待30多分鐘等待測試代理代碼。如果您想更頻繁地執行功能,則無法在設備上執行此功能。 –

相關問題