예제 #1
0
        /// <summary>
        ///   Executes the action on the UI thread asynchronously.
        /// </summary>
        /// <param name = "action">The action to execute.</param>
        public static Task OnUIThreadAsync(this System.Action action)
        {
            ValidateDispatcher();
#if WinRT
            return(dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask());
#elif NET45
            return(dispatcher.InvokeAsync(action).Task);
#else
            var           taskSource = new TaskCompletionSource <object>();
            System.Action method     = () =>
            {
                try
                {
                    action();
                    taskSource.SetResult(null);
                }
                catch (Exception ex)
                {
                    taskSource.SetException(ex);
                }
            };
            dispatcher.BeginInvoke(method);
            return(taskSource.Task);
#endif
        }
예제 #2
0
        /// <summary>
        /// Executes the action on the UI thread asynchronously.
        /// </summary>
        /// <param name="action">The action to execute.</param>
        /// <returns></returns>
        public virtual Task OnUIThreadAsync(Func <Task> action)
        {
            ValidateDispatcher();
#if WINDOWS_UWP
            return(dispatcher.RunTaskAsync(action));
#else
            return(dispatcher.InvokeAsync(action).Task.Unwrap());
#endif
        }
예제 #3
0
 private bool InvokeOrBeginInvoke(Action action)
 {
     // TODO - could consider using _uiDispatcher.get_HasThreadAccess()
     _uiDispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) => action(), this, null);
     return(true);
 }