/// <summary>Lifecycle suspend.</summary> /// <param name="sender">The source of the suspend request.</param> /// <param name="e">Details about the suspend request.</param> private async void OnSuspending(object sender, SuspendingEventArgs e) { LoggingService.LogInfo($"OnSuspending {e.SuspendingOperation.ToString()}", Windows.Foundation.Diagnostics.LoggingLevel.Information); // This is the most accurate thing that we can tell the user. // There is no way to know if the app is being terminated or just suspended for fun. if (_notPrelaunchSuspend) { LoggingService.LogInfo("App is suspending.", Windows.Foundation.Diagnostics.LoggingLevel.Information); } else { LoggingService.LogInfo("OnSuspending - Prelaunch", Windows.Foundation.Diagnostics.LoggingLevel.Information); } // If the deferral is not obtained the suspension proceeds at the end of this method. // With the deferral there is still a 5 second time limit to completing suspension code. // The deferral allows code to be awaited in this method. var deferral = e.SuspendingOperation.GetDeferral(); var completer = new TaskCompletionSource <object>(); AddSessionTask(completer.Task); await BroadcasterService.Instance.Suspending.Invoke(completer, true, true); await completer.Task; _networking?.Suspend(); DatapointService.Instance.Suspend(); WebSocketConnection.Instance.Suspend(); VirtualDeviceService.Stop(); deferral.Complete(); }