在我的Windows Phone 8应用程序中,我必须执行任务(LiveTiles和ToastNotifications)。我想将这些任务作为定期后台任务运行。 Toast Notification任务每天运行一次,LiveTiles任务每10分钟运行一次。添加第二个周期性任务时,显示错误(BNS错误:已添加此类型的最大ScheduledActions数)。如果您有解决方案,任何人都会告诉我答案。在这里,我附上了代码。
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 :(得分:3)
正如您所发现的那样,只有一个应用程序可以执行单个后台任务。
然而,该任务可以执行多个功能。如果你想让一天只发生一次,那就跟踪它是否已经在那天运行。
您还应注意,每10分钟无法执行一项任务 计划任务大约每30分钟运行一次(通常为+/- 10分钟),您无法控制它们运行的时间。它们由操作系统安排,以优化电池消耗。
如果您希望每10分钟更新一次您的磁贴,则需要通过推送通知执行此操作。
答案 1 :(得分:0)
我为我的问题找到了解决方案.Hereby我添加了代码。 在我的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分执行(来自我的样本)。