public async void Run(IBackgroundTaskInstance taskInstance) { // Create the baconit manager m_baconMan = new BaconManager(true); // Setup the ref counted deferral RefCountedDeferral refDeferral = new RefCountedDeferral(taskInstance.GetDeferral(), OnDeferralCleanup); // Add a ref so everyone in this call is protected refDeferral.AddRef(); // Fire off the update await m_baconMan.BackgroundMan.RunUpdate(refDeferral); // After this returns the deferral will call complete unless someone else took a ref on it. refDeferral.ReleaseRef(); }
/// <summary> /// Called by the app when the app is suspending /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OnSuspending_Fired(object sender, SuspendingEventArgs e) { // Setup a ref deferral for everyone to hold. We also need to setup a clean up action to save the setting // when the deferral is done. RefCountedDeferral refDeferral = new RefCountedDeferral(e.SuspendingOperation.GetDeferral(), () => { // We need to flush the settings here just before we complete the deferal. We need to block this function // until the settings are flushed. using (AutoResetEvent are = new AutoResetEvent(false)) { Task.Run(async () => { // Flush out the local settings await SettingsMan.FlushLocalSettings(); are.Set(); }); are.WaitOne(); } }); // Add a ref to cover anyone down this call stack. refDeferral.AddRef(); // Make the OnSuspendingArgs args = new OnSuspendingArgs() { RefDeferral = refDeferral }; // Fire the event m_onSuspending.Raise(this, args); // Release our ref to the deferral refDeferral.ReleaseRef(); }