BackgroundTask UWP Windows 10 TimeTriggeredTask Example registered but never fires up

旧时模样 提交于 2019-12-21 19:43:36

问题


I am testing the Background Example with the Scenario4 and it never calls to Run, I am following the steps and that's what I see:

1.- After calling

BackgroundTaskRegistration task = builder.Register();

The task has Trigger = null

Does it should be the trigger specified before in

builder.SetTrigger(trigger); ?

2.- In theory when I register a background task, it should appear in here EventViewer - BackgroundTaskInfrastructure but it doesn't, should appear here?

3.- To try to debug I Debug Location - Suspend the App but after more than 15 minutes it never arrives, should be simple as suspend and wait?

Also I added inside Run a notification:

var toastXML = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var toastTextElements = toastXML.GetElementsByTagName("text");
toastTextElements.First().AppendChild(toastXML.CreateTextNode("Hello World:"));
var toastNotification = new ToastNotification(toastXML);
toastNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(30);
ToastNotificationManager.CreateToastNotifier().Show(toastNotification);

But it never appears. So is BackgroundTask with TimeTrigger working?


回答1:


In my case I have an await call in the Run method of the BackgroundTask. After watching the video https://channel9.msdn.com/Events/Windows/Developers-Guide-to-Windows-10-RTM/Background-Execution I learn that I have to call to:

 var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;

And get the

 var deferral = taskInstance.GetDeferral();

And finally when my calls finish call to:

 deferral.Complete();

With that now it calls always my task always finish, the only issue and I think that it is not allowed is that I cannot call to:

  Launcher.LaunchUriAsync(new Uri(favorite.Uri, UriKind.Absolute));

might be the launcher is not allowed but the toast is now showed every 15 minutes.



来源:https://stackoverflow.com/questions/31807296/backgroundtask-uwp-windows-10-timetriggeredtask-example-registered-but-never-fir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!