ruNury
ruNury

Reputation: 173

How to perform critical operation on App termination (iOS: WillTerminate, Android:OnDestroy)

I'm working on a Xamarin Forms app that communicates with a hardware device over Bluetooth Low Energy (BLE).

If the app user decides to terminate the app by swiping up, I would like to notify the hardware device that the communication channel should be closed. The issue is that this communication is an async operation taking somewhere between 500ms and 1500ms. I don't care about the reply from the hardware device, as I have no time to act on it anyways, due to my 5 sec window before the app process is gone, but I would like to ensure that the "shutdown" operation is sent to the device.

So far I've tried this to get it run synchronously, but the app crashes when I put the .Wait() on Task.Run - withoug .Wait() the "shutdown" is never sent to the device. How would I go about ensuring getting this async call to the hardware device?

Android:

        protected override void OnDestroy()
        {
            Task.Run(() => _appRestartService?.ShutDown()).Wait();
            base.OnDestroy();
        }

iOS

        public override void WillTerminate(UIApplication application)
        {
            Task.Run(() => _appRestartService?.ShutDown()).Wait();
        }

Exception on iOS:

System.AggregateException: One or more errors occurred. (A task was canceled.) ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
   --- End of inner exception stack trace ---
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2029 
  at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2759 
  at System.Threading.Tasks.Task.Wait () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2625 
  at NameSpace.AppDelegate.WillTerminate (UIKit.UIApplication application) [0x00001] in /Users/X/src/App/AppProjectName/AppDelegate.cs:128 
  at (wrapper managed-to-native) UIKit.UIApplication.xamarin_UIApplicationMain(int,intptr,intptr,intptr,intptr&)
  at UIKit.UIApplication.UIApplicationMain (System.Int32 argc, System.String[] argv, System.IntPtr principalClassName, System.IntPtr delegateClassName) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.6/src/Xamarin.iOS/UIKit/UIApplication.cs:58 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.6/src/Xamarin.iOS/UIKit/UIApplication.cs:84 
  at AppProjectName.Application.Main (System.String[] args) [0x00002] in /Users/X/src/App/AppProjectName/Main.cs:18 
---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException: A task was canceled.<---

Thanks in advance!

Upvotes: 0

Views: 90

Answers (0)

Related Questions