/// <summary> /// Executes the specified delegate asynchronously at the specified priority with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <typeparam name="T">The type of the result.</typeparam> /// <param name="dispatcher">The dispatcher.</param> /// <param name="func">The function.</param> /// <param name="priority">The priority.</param> /// <returns>The task representing the action.</returns> public static Task<T> InvokeAsync<T>(this Dispatcher dispatcher, Func<T> func, DispatcherPriority priority) { return RunWithResultAsync(dispatcher, () => { return func(); }, priority); }
/// <summary> /// Executes the specified action asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="func">The async action.</param> /// <returns> /// The DispatcherOperation or <c>null</c> if the action was not dispatched but executed directly. /// </returns> /// <exception cref="ArgumentNullException">The <paramref name="func" /> is <c>null</c>.</exception> /// <remarks> /// For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead. /// </remarks> public static async Task BeginInvokeAsync(this Dispatcher dispatcher, Func <Task> func) { Argument.IsNotNull(() => dispatcher); Argument.IsNotNull(() => func); var tcs = new TaskCompletionSource <object>(); #pragma warning disable AvoidAsyncVoid // Avoid async void #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed dispatcher.BeginInvoke(async() => #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed #pragma warning restore AvoidAsyncVoid // Avoid async void { try { var task = func(); await task; tcs.TrySetResult(null); } catch (Exception ex) { tcs.TrySetException(ex); } }, false); await tcs.Task; }
private static Task RunAsync(this Dispatcher dispatcher, Action action, DispatcherPriority priority) { #if UWP var task = dispatcher.RunAsync(priority.ToCoreDispatcherPriority(), () => { action(); }); return task.AsTask(); #else var tcs = new TaskCompletionSource<bool>(); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { action(); SetResult(tcs, true); } catch (Exception ex) { tcs.SetException(ex); } }), priority, null); dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); return tcs.Task; #endif }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="onlyBeginInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise, /// <c>Dispatcher.BeginInvoke</c> will be used.</param> public static void BeginInvoke(this Dispatcher dispatcher, Action action, bool onlyBeginInvokeWhenNoAccess) { Argument.IsNotNull("action", action); bool actionInvoked = false; if (dispatcher != null) { if (!onlyBeginInvokeWhenNoAccess || !dispatcher.CheckAccess()) { #if NETFX_CORE dispatcher.BeginInvoke(action); #else dispatcher.BeginInvoke(action, null); #endif actionInvoked = true; } } if (!actionInvoked) { action.Invoke(); } }
private static async Task<T> RunWithResultAsync<T>(this Dispatcher dispatcher, Func<T> function, DispatcherPriority priority) { var result = default(T); #if UWP await dispatcher.RunAsync(priority.ToCoreDispatcherPriority(), () => { result = function(); }); #else var tcs = new TaskCompletionSource<T>(); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { result = function(); } catch (Exception ex) { tcs.SetException(ex); } }), priority, null); dispatcherOperation.Completed += (sender, e) => SetResult(tcs, result); dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); await tcs.Task; #endif return result; }
/// <summary> /// Executes the specified delegate asynchronously at the specified priority with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="method">The method.</param> /// <param name="priority">The priority.</param> /// <param name="args">The arguments to pass into the method.</param> /// <returns>The task representing the action.</returns> public static Task InvokeAsync(this Dispatcher dispatcher, Delegate method, DispatcherPriority priority, params object[] args) { return RunAsync(dispatcher, () => { method.DynamicInvoke(args); }, priority); }
private static async Task RunAsync(this Dispatcher dispatcher, Func<Task> actionAsync, DispatcherPriority priority) { await dispatcher.RunWithResultAsync(async token => { await actionAsync(); return (object)null; }, CancellationToken.None, priority); }
private static async Task RunAsync(this Dispatcher dispatcher, Func <CancellationToken, Task> actionAsync, CancellationToken cancellationToken, DispatcherPriority priority) { await dispatcher.RunWithResultAsync(async token => { await actionAsync(cancellationToken); return((object)null); }, cancellationToken, priority); }
private void Update_Dispatcher(global::Windows.UI.Core.CoreDispatcher obj, int phase) { if ((phase & ((1 << 0) | NOT_PHASED)) != 0) { // Views\MainPage.xaml line 28 if (!isobj4CommandParameterDisabled) { XamlBindingSetters.Set_Windows_UI_Xaml_Controls_Primitives_ButtonBase_CommandParameter(this.obj4, obj, null); } } }
private static async Task <T> RunWithResultAsync <T>(this Dispatcher dispatcher, Func <CancellationToken, Task <T> > functionAsync, CancellationToken cancellationToken, DispatcherPriority priority) { var tcs = new TaskCompletionSource <T>(); #if UWP throw Log.ErrorAndCreateException <PlatformNotSupportedException>(); #endif // Only invoke if we really have to if (dispatcher.CheckAccess()) { var result = await functionAsync(cancellationToken); return(result); } var dispatcherOperation = dispatcher.BeginInvoke(new Action(async() => { try { var task = functionAsync(cancellationToken); await task; if (task.IsFaulted) { tcs.TrySetException(task.Exception ?? new Exception("Unknown error")); return; } if (task.IsCanceled) { SetCanceled(tcs); return; } SetResult(tcs, task.Result); } catch (Exception ex) { // NOTE: in theory, it could have been already set before tcs.TrySetException(ex); } }), priority, null); // IMPORTANT: don't handle 'dispatcherOperation.Completed' event. // We should only signal to awaiter when the operation is really done dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); return(await tcs.Task); }
/// <summary> /// Executes the specified action synchronously at the specified priority with the specified arguments on the thread the Dispatcher is associated with. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="priority">The priority.</param> /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static void Invoke(this Dispatcher dispatcher, Action action, DispatcherPriority priority) { Argument.IsNotNull("action", action); if (dispatcher != null && !dispatcher.CheckAccess()) { dispatcher.Invoke(action, priority, null); } else { action.Invoke(); } }
/// <summary> /// Executes the specified delegate synchronously at the specified priority with the specified arguments on the thread the Dispatcher is associated with. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="method">A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.</param> /// <param name="priority">The priority.</param> /// <param name="args">An array of objects to pass as arguments to the given method. Can be <c>null</c>.</param> /// <exception cref="ArgumentNullException">The <paramref name="method" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static void Invoke(this Dispatcher dispatcher, Delegate method, DispatcherPriority priority, params object[] args) { Argument.IsNotNull("method", method); if (dispatcher != null && !dispatcher.CheckAccess()) { dispatcher.Invoke(method, priority, args); } else { method.DynamicInvoke(args); } }
/// <summary> /// Executes the specified delegate synchronously at the specified priority with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="priority">The priority.</param> /// <param name="onlyInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise, /// <c>Dispatcher.BeginInvoke</c> will be used.</param> public static void Invoke(this Dispatcher dispatcher, Action action, DispatcherPriority priority, bool onlyInvokeWhenNoAccess) { Argument.IsNotNull("action", action); if (dispatcher != null) { if (!onlyInvokeWhenNoAccess || !dispatcher.CheckAccess()) { dispatcher.Invoke(action, priority, null); return; } } action.Invoke(); }
/// <summary> /// Executes the specified delegate asynchronously at the specified priority with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="priority">The priority.</param> /// <param name="onlyBeginInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise, /// <c>Dispatcher.BeginInvoke</c> will be used.</param> /// <returns>The DispatcherOperation or <c>null</c> if the action was not dispatched but executed directly.</returns> public static DispatcherOperation BeginInvoke(this Dispatcher dispatcher, Action action, DispatcherPriority priority, bool onlyBeginInvokeWhenNoAccess) { Argument.IsNotNull("action", action); if (dispatcher != null) { if (!onlyBeginInvokeWhenNoAccess || !dispatcher.CheckAccess()) { return(dispatcher.BeginInvoke(action, priority, null)); } } action.Invoke(); return(GetDefaultDispatcherOperation(dispatcher)); }
private static async Task<T> RunWithResultAsync<T>(this Dispatcher dispatcher, Func<CancellationToken, Task<T>> functionAsync, CancellationToken cancellationToken, DispatcherPriority priority) { var tcs = new TaskCompletionSource<T>(); #if UWP await dispatcher.RunAsync(priority.ToCoreDispatcherPriority(), async () => #else var dispatcherOperation = dispatcher.BeginInvoke(new Action(async () => #endif { try { var task = functionAsync(cancellationToken); await task; if (task.IsFaulted) { tcs.TrySetException(task.Exception ?? new Exception("Unknown error")); return; } if (task.IsCanceled) { SetCanceled(tcs); return; } SetResult(tcs, task.Result); } catch (Exception ex) { // NOTE: in theory, it could have been already set before tcs.TrySetException(ex); } #if !UWP }), priority, null); // IMPORTANT: don't handle 'dispatcherOperation.Completed' event. // We should only signal to awaiter when the operation is really done dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); #else });
/// <summary> /// Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="method">A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.</param> /// <param name="args">An array of objects to pass as arguments to the given method. Can be <c>null</c>.</param> /// <exception cref="ArgumentNullException">The <paramref name="method" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static void Invoke(this Dispatcher dispatcher, Delegate method, params object[] args) { Argument.IsNotNull("method", method); if (dispatcher != null && !dispatcher.CheckAccess()) { #if NET dispatcher.Invoke(method, args); #elif NETFX_CORE dispatcher.BeginInvoke(() => method.DynamicInvoke(args)); #else dispatcher.BeginInvoke(method, args); #endif } else { method.DynamicInvoke(args); } }
/// <summary> /// Executes the specified action with the specified arguments synchronously on the thread the Dispatcher is associated with. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static void Invoke(this Dispatcher dispatcher, Action action) { Argument.IsNotNull("action", action); if (dispatcher != null && !dispatcher.CheckAccess()) { #if NET dispatcher.Invoke(action, null); #elif NETFX_CORE dispatcher.BeginInvoke(action); #else dispatcher.BeginInvoke(action); #endif } else { action.Invoke(); } }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="onlyBeginInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise, /// <c>Dispatcher.BeginInvoke</c> will be used.</param> /// <returns>The DispatcherOperation or <c>null</c> if the action was not dispatched but executed directly.</returns> public static DispatcherOperation BeginInvoke(this Dispatcher dispatcher, Action action, bool onlyBeginInvokeWhenNoAccess) { Argument.IsNotNull("action", action); if (dispatcher != null) { if (!onlyBeginInvokeWhenNoAccess || !dispatcher.CheckAccess()) { #if NETFX_CORE dispatcher.BeginInvoke(action); return(DispatcherOperation.Default); #else return(dispatcher.BeginInvoke(action, null)); #endif } } action.Invoke(); return(null); }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <param name="onlyBeginInvokeWhenNoAccess">If set to <c>true</c>, the action will be executed directly if possible. Otherwise, /// <c>Dispatcher.BeginInvoke</c> will be used.</param> public static void Invoke(this Dispatcher dispatcher, Action action, bool onlyBeginInvokeWhenNoAccess) { Argument.IsNotNull("action", action); if (dispatcher != null) { if (!onlyBeginInvokeWhenNoAccess || !dispatcher.CheckAccess()) { #if UWP dispatcher.Invoke(action); return; #else dispatcher.Invoke(action, null); return; #endif } } action.Invoke(); }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <returns>The task representing the action.</returns> public static Task InvokeAsync(this Dispatcher dispatcher, Action action) { var tcs = new TaskCompletionSource<bool>(); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { action(); } catch (Exception ex) { tcs.SetException(ex); } }), null); dispatcherOperation.Completed += (sender, e) => SetResult(tcs, true); dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); return tcs.Task; }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="method">The method.</param> /// <param name="args">The arguments to pass into the method.</param> /// <returns>The task representing the action.</returns> public static Task InvokeAsync(this Dispatcher dispatcher, Delegate method, params object[] args) { var tcs = new TaskCompletionSource <bool>(); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { method.DynamicInvoke(args); } catch (Exception ex) { tcs.SetException(ex); } }), null); dispatcherOperation.Completed += (sender, e) => SetResult(tcs, true); dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); return(tcs.Task); }
private static async Task <T> RunWithResultAsync <T>(this Dispatcher dispatcher, Func <T> function, DispatcherPriority priority) { var result = default(T); #if UWP throw Log.ErrorAndCreateException <PlatformNotSupportedException>(); #endif // Only invoke if we really have to if (dispatcher.CheckAccess()) { result = function(); return(result); } var tcs = new TaskCompletionSource <T>(); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { result = function(); SetResult(tcs, result); } catch (Exception ex) { tcs.SetException(ex); } }), priority, null); // IMPORTANT: don't handle 'dispatcherOperation.Completed' event. // We should only signal to awaiter when the operation is really done dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); await tcs.Task; return(result); }
private static DispatcherOperation GetDefaultDispatcherOperation(Dispatcher dispatcher) { // Fix for https://github.com/Catel/Catel/issues/1220 #if UWP return(DispatcherOperation.Default); #else //[SecurityCritical] //internal DispatcherOperation(Dispatcher dispatcher, DispatcherPriority priority, Action action) //: this(dispatcher, (Delegate)action, priority, (object)null, 0, (DispatcherOperationTaskSource)new DispatcherOperationTaskSource<object>(), true) //{ //} //var dispatcherOperation = (DispatcherOperation)Activator.CreateInstance(typeof(DispatcherOperation), // dispatcher, DispatcherPriority.Normal, EmptyAction); //return dispatcherOperation; // Unfortunately we will need to await a dispatcher operation anyway return(dispatcher.BeginInvoke(EmptyAction)); #endif }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <typeparam name="T">The type of the result.</typeparam> /// <param name="dispatcher">The dispatcher.</param> /// <param name="func">The function.</param> /// <returns>The task representing the action.</returns> public static Task <T> InvokeAsync <T>(this Dispatcher dispatcher, Func <T> func) { var tcs = new TaskCompletionSource <T>(); var result = default(T); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { result = func(); } catch (Exception ex) { tcs.SetException(ex); } }), null); dispatcherOperation.Completed += (sender, e) => SetResult(tcs, result); dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); return(tcs.Task); }
private static Task RunAsync(this Dispatcher dispatcher, Action action, DispatcherPriority priority) { #if UWP throw Log.ErrorAndCreateException <PlatformNotSupportedException>(); #endif // Only invoke if we really have to if (dispatcher.CheckAccess()) { action(); return(TaskHelper.Completed); } var tcs = new TaskCompletionSource <bool>(); var dispatcherOperation = dispatcher.BeginInvoke(new Action(() => { try { action(); SetResult(tcs, true); } catch (Exception ex) { tcs.SetException(ex); } }), priority, null); // IMPORTANT: don't handle 'dispatcherOperation.Completed' event. // We should only signal to awaiter when the operation is really done dispatcherOperation.Aborted += (sender, e) => SetCanceled(tcs); return(tcs.Task); }
/// <summary> /// Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on if required. /// <para /> /// To check whether this is necessary, it will check whether the current thread has access to the dispatcher. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="method">A delegate to a method that takes parameters specified in args, which is pushed onto the Dispatcher event queue.</param> /// <param name="args">An array of objects to pass as arguments to the given method. Can be <c>null</c>.</param> /// <returns>The DispatcherOperation or <c>null</c> if the action was not dispatched but executed directly.</returns> /// <exception cref="ArgumentNullException">The <paramref name="method" /> is <c>null</c>.</exception> public static DispatcherOperation BeginInvokeIfRequired(this Dispatcher dispatcher, Delegate method, params object[] args) { Argument.IsNotNull("method", method); return(BeginInvoke(dispatcher, () => method.DynamicInvoke(args), true)); }
/// <summary> /// Executes the specified action asynchronously with the specified arguments on the thread that the Dispatcher was created on if required. /// <para /> /// To check whether this is necessary, it will check whether the current thread has access to the dispatcher. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <returns>The DispatcherOperation or <c>null</c> if the action was not dispatched but executed directly.</returns> /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static DispatcherOperation BeginInvokeIfRequired(this Dispatcher dispatcher, Action action) { return(BeginInvoke(dispatcher, action, true)); }
/// <summary> /// Executes the specified action asynchronously with the specified arguments on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="action">The action.</param> /// <returns>The DispatcherOperation or <c>null</c> if the action was not dispatched but executed directly.</returns> /// <exception cref="ArgumentNullException">The <paramref name="action" /> is <c>null</c>.</exception> /// <remarks>For target frameworks where the <see cref="Dispatcher" /> class does not contain the <c>Invoke</c> method, the <c>BeginInvoke</c> /// method will be used instead.</remarks> public static DispatcherOperation BeginInvoke(this Dispatcher dispatcher, Action action) { return(BeginInvoke(dispatcher, action, false)); }
/// <summary> /// Executes the specified asynchronous operation on the thread that the Dispatcher was created on with supporting of cancellation token. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="actionAsync">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The task representing the asynchronous operation.</returns> public static Task InvokeAsync(this Dispatcher dispatcher, Func<CancellationToken, Task> actionAsync, CancellationToken cancellationToken) { return RunAsync(dispatcher, actionAsync, cancellationToken, DispatcherPriority.Normal); }
/// <summary> /// Executes the specified asynchronous operation on the thread that the Dispatcher was created on. /// </summary> /// <param name="dispatcher">The dispatcher.</param> /// <param name="actionAsync">The asynchronous operation without returning a value.</param> /// <returns>The task representing the asynchronous operation.</returns> public static Task InvokeAsync(this Dispatcher dispatcher, Func<Task> actionAsync) { return RunAsync(dispatcher, actionAsync, DispatcherPriority.Normal); }