我的定期任务(即后台代理)在 Windows Phone 7.1 中不会一次又一次被调用
我创建了一个简单的定期任务,每 15 秒显示一条 toast 消息。但它只显示一次吐司,再也不会显示。我仅在调试模式下运行该项目。 这是我的代码。我在哪里犯了错误?
//这是主页代码
private void button1_Click(object sender, RoutedEventArgs e)
{
// A unique name for your task. It is used to
// locate it in from the service.
var TASK_NAME = "ScheduledTaskAgent1";
PeriodicTask task = new PeriodicTask(TASK_NAME);
task.Description = "This is our custom agent for Day 25 - Background Agents";
ScheduledActionService.Add(task);
ScheduledActionService.LaunchForTest(TASK_NAME,
TimeSpan.FromMilliseconds(1500));
}
//这是 SCHEDULEDTASKAGENT 代码
protected override void OnInvoke(ScheduledTask task)
{
//TODO: Add code to perform your task in background
ShellToast popupMessage = new ShellToast()
{
Title = "My First Agent",
Content = "Background Task Launched",
};
popupMessage.Show();
NotifyComplete();
}
I created a simple periodic task that will show a toast message every 15 seconds. But it just shows the toast only once and never again. I am running the project in the debug mode only.
Here is my code. Where am i making a mistake?
//THIS IS THE MAINPAGE CODE
private void button1_Click(object sender, RoutedEventArgs e)
{
// A unique name for your task. It is used to
// locate it in from the service.
var TASK_NAME = "ScheduledTaskAgent1";
PeriodicTask task = new PeriodicTask(TASK_NAME);
task.Description = "This is our custom agent for Day 25 - Background Agents";
ScheduledActionService.Add(task);
ScheduledActionService.LaunchForTest(TASK_NAME,
TimeSpan.FromMilliseconds(1500));
}
//THIS IS THE SCHEDULEDTASKAGENT CODE
protected override void OnInvoke(ScheduledTask task)
{
//TODO: Add code to perform your task in background
ShellToast popupMessage = new ShellToast()
{
Title = "My First Agent",
Content = "Background Task Launched",
};
popupMessage.Show();
NotifyComplete();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LaunchForTest 在指定的延迟后调用您的任务一次,顺便说一句,您已指定为 1.5 秒而不是 15 秒。在 OnInvoke 中,在 NotifyComplete(); 之前添加以下内容;
不要忘记,LaunchForTest 旨在用于开发,不应在发布版本中使用。
LaunchForTest invokes your task once after the specified delay which, by the way, you have specified as 1.5 seconds not 15. In your OnInvoke, add the folowing before NotifyComplete();
Don't forget, LaunchForTest is intended for development and should not be used in a release build.
您不能如此频繁地调用任务。系统会在大约 28 分钟内启动您的代理。此处列出了定期代理的约束:https://msdn.microsoft.com/en-us/library/windows/apps/hh202942(v=vs.105).aspx#BKMK_ConstraintsforPeriodicAgents
You can't invoke a task so often. The system launches your agent in ~28 mins. Constraints for periodic agents are listed here: https://msdn.microsoft.com/en-us/library/windows/apps/hh202942(v=vs.105).aspx#BKMK_ConstraintsforPeriodicAgents