C# 如何在wp8应用程序中定期运行两个后台任务?
在我的Windows Phone 8应用程序中,我必须完成任务(LiveTiles和ToastNotifications)。我希望将这些任务作为定期后台任务运行。Toast通知任务每天运行一次,my LiveTiles任务每10分钟运行一次。添加第二个定期任务时,它显示一个错误(BNS错误:已添加此类型的最大计划数)。如果你有解决办法,任何人都可以告诉我答案。这里我附上了代码 App.xaml.cs:C# 如何在wp8应用程序中定期运行两个后台任务?,c#,windows-phone-8,C#,Windows Phone 8,在我的Windows Phone 8应用程序中,我必须完成任务(LiveTiles和ToastNotifications)。我希望将这些任务作为定期后台任务运行。Toast通知任务每天运行一次,my LiveTiles任务每10分钟运行一次。添加第二个定期任务时,它显示一个错误(BNS错误:已添加此类型的最大计划数)。如果你有解决办法,任何人都可以告诉我答案。这里我附上了代码 App.xaml.cs: var LiveTilesName = "LiveTiles"; va
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分钟更新一次互动程序,您需要通过推送通知来完成。正如您所发现的,应用程序只能有一个后台任务 但是,可以让该任务执行多种功能。如果你想一天只发生一次,只需记录当天是否已经运行过 您还应注意,不可能每10分钟运行一次任务。
计划任务大约每30分钟运行一次(通常为+/-10分钟),您无法控制它们运行的时间。它们由操作系统安排,以优化电池消耗
如果您想每10分钟更新一次互动程序,您需要通过推送通知来完成此操作。我找到了问题的解决方案。在此,我添加了代码。 在我的App.xaml.cs文件中,我有一个名为StartAgentTask()的方法
private void StartAgentTask()
{
var LiveTilesName=“LiveTiles”;
var ToastNotificationsName=“ToastNotifications”;
PeriodicTask LiveTilesPeriodicTask=ScheduledActionService.Find(LiveTilesName)作为PeriodicTask;
ResourceIntensificationTask ToastNotificationResourceIntensificationTask=ScheduledActionService.Find(ToastNotificationsName)作为ResourceIntensificationTask;
if(liveTilesPeriodictTask!=null)
ScheduledActionService.Remove(LiveTilesName);
if(ToastNotificationResourceIntensiveTask!=null)
ScheduledActionService.Remove(到astNotificationsName);
LiveTilesPeriodictTask=新的PeriodictTask(LiveTilesName){Description=“Update Live Tiles.”;
ToastNotificationResourceIntensiveTask=新的ResourceIntensiveTask(ToastNotificationsName){Description=“Toast通知”};
尝试
{
ScheduledActionService.Add(LiveTilesPeriodictTask);
ScheduledActionService.LaunchForTest(LiveTilesName,TimeSpan.FromSeconds(10));
ScheduledActionService.Add(ToastNotificationResourceIntensiveTask);
双秒;
如果(DateTime.Now.TimeOfDay我找到了问题的解决方案。在此我添加了代码。
在我的App.xaml.cs文件中,我有一个名为StartAgentTask()的方法
private void StartAgentTask()
{
var LiveTilesName=“LiveTiles”;
var ToastNotificationsName=“ToastNotifications”;
PeriodicTask LiveTilesPeriodicTask=ScheduledActionService.Find(LiveTilesName)作为PeriodicTask;
ResourceIntensificationTask ToastNotificationResourceIntensificationTask=ScheduledActionService.Find(ToastNotificationsName)作为ResourceIntensificationTask;
if(liveTilesPeriodictTask!=null)
ScheduledActionService.Remove(LiveTilesName);
if(ToastNotificationResourceIntensiveTask!=null)
ScheduledActionService.Remove(到astNotificationsName);
LiveTilesPeriodictTask=新的PeriodictTask(LiveTilesName){Description=“Update Live Tiles.”;
ToastNotificationResourceIntensiveTask=新的ResourceIntensiveTask(ToastNotificationsName){Description=“Toast通知”};
尝试
{
ScheduledActionService.Add(LiveTilesPeriodictTask);
ScheduledActionService.LaunchForTest(LiveTilesName,TimeSpan.FromSeconds(10));
ScheduledActionService.Add(ToastNotificationResourceIntensiveTask);
双秒;
如果(DateTime.Now.TimeOfDay感谢Matt Lacey,我找到了解决方案。我会很快上传我的工作代码。感谢Matt Lacey,我找到了解决方案。我会很快上传我的工作代码。请注意,ScheduledActionService.LaunchForTest
只能在调试条件下使用。它不能在发布代码中使用,也不能在提交的应用中使用到商店。好的,Matt,那么什么是解决方案?如果你有,请详细解释。你基本上做了你已经做过的事情,但需要依赖于代理调用的自动调度。LaunchForTest
只是一个帮助方法辅助测试。这样你就不必等待30多分钟来测试代理代码。如果你想要更频繁地执行功能,您不能在设备上执行。Matt!我理解您在第一条评论中所说的。我正在询问您是否有针对这种情况的其他解决方案。请注意,ScheduledActionService.LaunchForTest
只能在调试条件下使用。它不能在发布代码中使用,也不能用于be用于提交到应用商店的应用程序。好的,Matt,那么什么是解决方案?如果你有,请详细解释。你基本上做了你已经做过的事情,但需要依赖于代理调用的自动调度。LaunchForTest
只是一个帮助方法辅助测试。这样你就不必等待30多分钟等待测试st代理代码。如果要执行更多功能
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) { }
}
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();
}