public async void Run(IBackgroundTaskInstance taskInstance) { // Don't place any code (including debug code) before GetDeferral!!! // Get a deferral, to prevent the task from closing prematurely // while asynchronous code is still running. deferral = taskInstance.GetDeferral(); taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); var tbtLog = await ApplicationData.Current.LocalFolder.CreateFileAsync("UserAwayBackTaskLog.txt", CreationCollisionOption.ReplaceExisting); var s = await tbtLog.OpenStreamForWriteAsync(); var sw = new StreamWriter(s); sw.WriteLine("Background task start time: " + DateTime.Now.ToString()); try { sw.WriteLine("Unregister all background tasks start: " + DateTime.Now.ToString()); BackTaskReg.unregisterBackTask("TimerTaq.BackTask"); BackTaskReg.unregisterBackTask("HasNetTaq.BackTask"); sw.WriteLine("Unregister all background tasks end: " + DateTime.Now.ToString()); } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { sw.WriteLine("Background task end time: " + DateTime.Now.ToString()); sw.Flush(); s.Dispose(); // Inform the system that the task is finished. deferral.Complete(); } }
public async Task <int> backTaskUpdateTiles() { if (isUpdateCompleted == true) { isUpdateCompleted = false; ApplicationTrigger trigger = new ApplicationTrigger(); var btr = await BackTaskReg.RegisterBackgroundTask("BackTaskUpdateTiles", "Taq.BackTask.BackTaskUpdateTiles", trigger); btr.Completed += Btr_Completed; var result = await trigger.RequestAsync(); } return(0); }
public async void Run(IBackgroundTaskInstance taskInstance) { // Don't place any code (including debug code) before GetDeferral!!! // Get a deferral, to prevent the task from closing prematurely // while asynchronous code is still running. deferral = taskInstance.GetDeferral(); taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled); var tbtLog = await ApplicationData.Current.LocalFolder.CreateFileAsync("AfterUpdateLog.txt", CreationCollisionOption.ReplaceExisting); var s = await tbtLog.OpenStreamForWriteAsync(); var sw = new StreamWriter(s); sw.WriteLine("Background task start time: " + DateTime.Now.ToString()); try { sw.WriteLine("Unregister old background tasks start: " + DateTime.Now.ToString()); foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name != "AfterUpdateBackTask") { task.Value.Unregister(true); } } sw.WriteLine("Unregister old background tasks end: " + DateTime.Now.ToString()); sw.WriteLine("Register new background tasks start: " + DateTime.Now.ToString()); /* * await BackTaskReg.RegisterBackgroundTask("UserPresentBackTask", "Taq.BackTask.UserPresentBackTask", new SystemTrigger(SystemTriggerType.UserPresent, false)); * await BackTaskReg.RegisterBackgroundTask("UserAwayBackTask", "Taq.BackTask.UserAwayBackTask", new SystemTrigger(SystemTriggerType.UserAway, false));*/ await BackTaskReg.UserPresentTaskReg(Convert.ToUInt32(localSettings.Values["BgUpdatePeriod"])); sw.WriteLine("Register new background tasks end: " + DateTime.Now.ToString()); } catch (Exception ex) { Debug.WriteLine(ex.Message); } finally { sw.WriteLine("Background task end time: " + DateTime.Now.ToString()); sw.Flush(); s.Dispose(); // Inform the system that the task is finished. deferral.Complete(); } }
public async Task <int> UserPresentTaskReg(uint timerPeriod) { // Update by timer. var btr = await BackTaskReg.backUpdateReg("Timer", new TimeTrigger(timerPeriod, false)); btr.Completed += Btr_Completed; // Update if the Internet is available. /* * var btr2 = await BackTaskReg.backUpdateReg("HasNet", new SystemTrigger(SystemTriggerType.InternetAvailable, false)); * btr2.Completed += Btr_Completed; * var btr3 = await BackTaskReg.backUpdateReg("UserPresent", new SystemTrigger(SystemTriggerType.UserPresent, false)); * btr3.Completed += Btr_Completed;*/ return(0); }
async Task <int> initBackTask() { if (app.vm.m.localSettings.Values["BgUpdatePeriod"] == null) { app.vm.BgUpdatePeriodId = 0; } else { app.vm.BgUpdatePeriodId = app.vm.bgUpdatePeriods.FindIndex(x => x == (int)app.vm.m.localSettings.Values["BgUpdatePeriod"]); } if (app.vm.m.localSettings.Values["BgMainSiteAutoPos"] == null) { app.vm.m.localSettings.Values["BgMainSiteAutoPos"] = false; } await BackTaskReg.RegisterBackgroundTask("AfterUpdateBackTask", "Taq.BackTask.AfterUpdate", new SystemTrigger(SystemTriggerType.ServicingComplete, false)); //await BackTaskReg.RegisterBackgroundTask("UserPresentBackTask", "Taq.BackTask.UserPresentBackTask", new SystemTrigger(SystemTriggerType.UserPresent, false)); // Update if user aways. //await BackTaskReg.RegisterBackgroundTask("UserAwayBackTask", "Taq.BackTask.UserAwayBackTask", new SystemTrigger(SystemTriggerType.UserAway, false)); await UserPresentTaskReg(Convert.ToUInt32(app.vm.m.localSettings.Values["BgUpdatePeriod"])); return(0); }