C# Windows Phone 8.1 SL推送通知后台任务运行方法过早退出。但它在一个示例演示项目中运行良好
为了在后台保存C# Windows Phone 8.1 SL推送通知后台任务运行方法过早退出。但它在一个示例演示项目中运行良好,c#,windows-phone-8,windows-phone-8.1,backgroundworker,C#,Windows Phone 8,Windows Phone 8.1,Backgroundworker,为了在后台保存RAW通知负载,我添加了一个Windows运行时组件项目来实现后台任务执行。 我设置了一个名为“BgTask”的类,并实现了IBackgroundTask和Run方法 namespace MyBgTask { public sealed class BgTask : IBackgroundTask { public void Run(IBackgroundTaskInstance taskInstance) {
RAW通知
负载,我添加了一个Windows运行时组件
项目来实现后台任务执行。
我设置了一个名为“BgTask”的类,并实现了IBackgroundTask
和Run
方法
namespace MyBgTask
{
public sealed class BgTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var a = taskInstance.GetDeferral();
string taskName = taskInstance.Task.Name;
Debug.WriteLine("Background " + taskName + " starting...");
// Store the content received from the notification so it can be retrieved from the UI.
//RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
//var pushdata = JsonConvert.DeserializeObject<PushModelData>(notification.Content);
//var pushdata = notification.Content;
var pushdata = "anoop";
if (settings.Values.ContainsKey(taskName))
{
var dataaa = settings.Values[taskName].ToString();
Debug.WriteLine(dataaa);
var lst = JsonConvert.DeserializeObject<List<string>>(dataaa);
lst.Add(pushdata);
var seri = JsonConvert.SerializeObject(lst);
Debug.WriteLine("saving >>> " + seri);
settings.Values[taskName] = seri;
}
else
{
var lst = new List<string>();
lst.Add(pushdata);
var seri = JsonConvert.SerializeObject(lst);
Debug.WriteLine("saving >>> " + pushdata);
settings.Values[taskName] = seri;
}
Debug.WriteLine("Background " + taskName + " completed!");
a.Complete();
}
}
}
我在主页上注册了这个任务
private async void Button_Click(object sender, RoutedEventArgs e)
{
BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
{
try
{
PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += OnPushNotification;
UnregisterBackgroundTask();
RegisterBackgroundTask();
}
catch (Exception ex)
{
// ... TODO
}
}
}
private void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
}
private void RegisterBackgroundTask()
{
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.TaskEntryPoint = "MyBgTask.BgTask";
taskBuilder.Name = "BgTask";
PushNotificationTrigger trigger = new PushNotificationTrigger();
taskBuilder.SetTrigger(trigger);
try
{
BackgroundTaskRegistration task = taskBuilder.Register();
task.Progress += task_Progress;
task.Completed += BackgroundTaskCompleted;
Debug.WriteLine("Registration success: ");
}
catch (Exception ex)
{
Debug.WriteLine("Registration error: " + ex.Message);
UnregisterBackgroundTask();
}
}
void task_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Debug.WriteLine("progress");
});
}
private void BackgroundTaskCompleted(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Background work item triggered by raw notification with payload = " + " has completed!");
});
}
private bool UnregisterBackgroundTask()
{
foreach (var iter in BackgroundTaskRegistration.AllTasks)
{
IBackgroundTaskRegistration task = iter.Value;
if (task.Name == "BgTask")
{
task.Unregister(true);
return true;
}
}
return false;
}
在Packagemanifest中设置入口点。
当我向包含大量代码的主项目发送原始通知时,pushnotification事件将触发。但问题是,当事件触发时,Run方法内的代码块没有触发。它会随消息自动退出
程序“[4484]BACKGROUNDTASKHOST.EXE”已被删除
已退出,代码为1(0x1)
每次都会发生。我创建了另一个空白的windows phone silverlight项目示例,并实现了后台任务。我通过发送原始通知进行了测试,现在它运行良好,Run方法中的代码块运行良好。请注意,这两个项目位于同一个解决方案中
我需要澄清以下几点
1.为什么后台任务在进入Run方法时自动退出?
2.这是因为我的项目较大还是占用了大量的应用程序内存?
3.我如何确定确切的原因
请帮我找出一个解决方案,让它按预期工作
非常感谢我认为问题不在于你的背景任务
你可以忽略现有代码1
我认为你的问题是:
您正在告诉操作系统创建一个新触发器,当操作系统收到一个推送通知时,该触发器将启动一个新的后台任务。此BackroundTask
或多或少是一个完整的应用程序,将由操作系统启动。应用程序和BackgroundTask共享的视图之一是ApplicationData.Current.LocalSettings
,您可以在应用程序和BackgroundTask
之间交换数据
创建/注册触发器时,您会得到一个BackgroundTaskRegistration
对象,您可以使用该对象将当前运行的应用程序绑定到已完成的事件
只要你的应用程序正在运行,一切正常,但如果你的应用程序被挂起或关闭,你的应用程序可能会丢失已完成事件的绑定
如果您想在应用程序未运行时向用户显示通知,请在背景任务中执行此操作
如果您需要从后台任务获取数据,即更新您的本地数据。将其保存到ApplicationData.Current.LocalSettings
,并在每次应用程序启动或恢复时检查它和事件绑定
请记住简化背景任务
,因为每个背景任务每小时只有几秒钟的计算时间。您能发布失败的代码吗?那一大块黄色突出显示的文本没有帮助。@PeterTorr MSFT Hai,我用代码更新了问题。@PeterTorr MSFT我用系统触发器测试过。但问题仍然存在。@asitis想知道你是否曾经让它工作过?我在执行触发器时遇到问题(只有当它是PushNotificationTrigger时,否则,如果我将其更改为SystemTrigger,它就会工作)。。。
private async void Button_Click(object sender, RoutedEventArgs e)
{
BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
{
try
{
PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += OnPushNotification;
UnregisterBackgroundTask();
RegisterBackgroundTask();
}
catch (Exception ex)
{
// ... TODO
}
}
}
private void OnPushNotification(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
}
private void RegisterBackgroundTask()
{
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.TaskEntryPoint = "MyBgTask.BgTask";
taskBuilder.Name = "BgTask";
PushNotificationTrigger trigger = new PushNotificationTrigger();
taskBuilder.SetTrigger(trigger);
try
{
BackgroundTaskRegistration task = taskBuilder.Register();
task.Progress += task_Progress;
task.Completed += BackgroundTaskCompleted;
Debug.WriteLine("Registration success: ");
}
catch (Exception ex)
{
Debug.WriteLine("Registration error: " + ex.Message);
UnregisterBackgroundTask();
}
}
void task_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Debug.WriteLine("progress");
});
}
private void BackgroundTaskCompleted(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Background work item triggered by raw notification with payload = " + " has completed!");
});
}
private bool UnregisterBackgroundTask()
{
foreach (var iter in BackgroundTaskRegistration.AllTasks)
{
IBackgroundTaskRegistration task = iter.Value;
if (task.Name == "BgTask")
{
task.Unregister(true);
return true;
}
}
return false;
}