#pragma warning disable 1998
        static public async void Run(VoidDelegate action)
        {
#if WINDOWS_DESKTOP
            if (Dispatcher == null)
            {
                action();
            }
            else
            {
                await Dispatcher.BeginInvoke(action);
            }
#elif WINDOWS_PHONE_APP
            if (Dispatcher != null)
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
            }
#elif WINDOWS_PHONE
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(action);
#elif __ANDROID__
            if (Activity != null)
            {
                Activity.RunOnUiThread(new Java.Lang.Runnable(() =>
                {
                    action();
                }));
            }
#elif __IOS__
            if (uiObject != null)
            {
                uiObject.InvokeOnMainThread(action);
            }
#endif
        }
Exemple #2
0
        public static Task OnMainThread(Action action)
        {
            var obj = new Foundation.NSObject();

            obj.InvokeOnMainThread(action);
            return(Task.FromResult(true));
        }
Exemple #3
0
        public static Task OnMainThread(Func <Task> action)
        {
            var tcs = new TaskCompletionSource <bool>();
            var obj = new Foundation.NSObject();

            obj.InvokeOnMainThread(async() =>
            {
                try
                {
                    await action();
                    tcs.SetResult(true);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            });
            return(tcs.Task);
        }
 public static void SafeInvokeOnMainThread(this Foundation.NSObject nsObject, Action action, Action <Exception> failureAction = null)
 {
     nsObject.InvokeOnMainThread(() => SafeInvoke(((Exception error) => LogUtils.Error(error)), failureAction, action));
 }