[英]Windows Phone 8.1 Background Agent invokes a custom method severally
我的Windows Phone 8.1(C#)有一个后台代理,可监听推送通知。 我也有一个前台应用程序,如果正在运行,它可以处理推送通知。 问题是后台代理调用自定义方法以await SyncPushChanges.initUpdate(true);
几次导致我的sqlite数据库中出现重复值。 这是前台委托的代码:
static async void channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
//Init update from the server
await SyncPushChanges.initUpdate();
//Prevent background agentt from being invoked
args.Cancel = true;
}
Code In background Agent
public async void Run(IBackgroundTaskInstance taskInstance)
{
var deferal = taskInstance.GetDeferral();//Save on CPU seconds since we are doing async
//Get the token and invoke get new data
await SyncPushChanges.initUpdate(true);
deferal.Complete();
}
任何人可能知道为什么要多次调用我的方法?我们将不胜感激您的帮助
我自己找到了解决方案。 问题是,我没有注销我的后台代理,这导致当应用程序被更新,它无法正常工作(在这种情况下,当我从VS2013再次运行应用程序)。 下面的代码做了魔术
BackgroundExecutionManager.RemoveAccess();
//Unregister the Background Agent
var entry = BackgroundTaskRegistration.AllTasks.FirstOrDefault(keyval => keyval.Value.Name == "myAgent");
if (entry.Value != null)
{
entry.Value.Unregister(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.