我下面https://docs.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task文件創建,將每15分鐘運行Ping一個服務後臺任務5次後沒有運行。UWP後臺任務
創建與實施IBackgroundTask
公共密封類在Windows運行時組件項目namespace PeriodicBackgroundTask { public sealed class FifteenMinutePeriodicTimer : IBackgroundTask { private static readonly FileLogWriter mWriteLogToFile = new FileLogWriter(); public FifteenMinutePeriodicTimer() { } public void Run(IBackgroundTaskInstance taskInstance) { try { taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); mDeferral = taskInstance.GetDeferral(); mWriteLogToFile.log("Starting periodic timer at " + DateTime.Now.ToString()); // Calling method to do a Post call to a service. mWriteLogToFile.log("Finishing periodic timer at " + DateTime.Now.ToString()); } catch (Exception ex) { mWriteLogToFile.log("Fifteen Minute periodic timer failed.", ex); } finally { // Adding some buffer for async processes to finish. Task.Delay(TimeSpan.FromSeconds(15)).Wait(); mDeferral.Complete(); } } private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { mWriteLogToFile.log("Fifteen minute periodic timer is canceled. {0}", reason.ToString()); } } }
項在UWP應用程序的文件Package.appmanifest
<Extension Category="windows.backgroundTasks" EntryPoint="PeriodicBackgroundTask.FifteenMinutePeriodicTimer"> <BackgroundTasks> <Task Type="systemEvent" /> <Task Type="timer" /> </BackgroundTasks> </Extension>
登記定時器之初在App.xaml.cs
OnLaunched方法public async static Task RegisterBackgroundTask() { foreach (var task in BackgroundTaskRegistration.AllTasks) { if (String.Equals(task.Value.Name, TASK_NAME)) { mWriteLogToFile.log("Old version of fifteen minute periodic timer found. Unregistering it."); BackgroundExecutionManager.RemoveAccess(); // Unregistering so that any update in Background task can be applied. task.Value.Unregister(true); break; } } BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); if (backgroundAccessStatus == BackgroundAccessStatus.DeniedByUser || backgroundAccessStatus == BackgroundAccessStatus.DeniedBySystemPolicy) { mWriteLogToFile.log("Fifteen minute periodic timer registration failed, due to Request access status."); return; } BackgroundTaskBuilder builder = new BackgroundTaskBuilder(); builder.Name = TASK_NAME; builder.TaskEntryPoint = "PeriodicBackgroundTask.FifteenMinutePeriodicTimer"; builder.SetTrigger(new TimeTrigger(15, false)); builder.CancelOnConditionLoss = false; var backgroundTask = builder.Register(); if (backgroundTask != null) { mWriteLogToFile.log("Fifteen minute periodic timer registration SUCCESSFUL."); } else { mWriteLogToFile.log("Fifteen minute periodic timer registration FAILED."); } }
安裝應用後FifteenMinuteBackgroundTimer運行5或6次。有時,5,6,有時當我嘗試使用Visual Studio調試任務我能調試。我不知道,爲什麼FifteenMinuteBackgroundTimer是5次運行後死亡。任何人都可以告訴我如何調試這個問題?
更多信息:
的Windows版本號:1607
Microsoft.NETCore.UniversalWindowsPlatform:5.1.0
感謝響應。我嘗試了你的建議並取得了一些進展。 如果我將設備始終保持打開狀態,那麼週期性定時器正在運行。但是,如果我鎖定屏幕並拔下它,定期計時器不會運行。 – c2tarun
@ c2tarun - 你可以進入設置,系統,電池,電池按應用程序,並更改你的應用程序'一直運行'。不理想,但它可能會給你一個指示,如果這是問題。 –
感謝您的回覆,我使用我的應用程序LockScreen應用程序從此文檔:https://msdn.microsoft.com/en-us/library/windows/apps/hh700416.aspx?f=255&MSPPError=-2147217396我認爲解決了後臺任務無法在鎖定屏幕時運行的問題。但奇怪的是,當任務正在運行時,它無法進行網絡通話。我認爲這可能是相關的東西http://stackoverflow.com/questions/39133667/uwp-background-task-httpclient-fails-when-device-screen-is-off – c2tarun