/// <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 }
/// <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 }
private bool InvokeOrBeginInvoke(Action action) { // TODO - could consider using _uiDispatcher.get_HasThreadAccess() _uiDispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) => action(), this, null); return(true); }