浏览代码

Added Dispatcher.InvokeTaskAsync helpers

Nikita Tsukanov 2 年之前
父节点
当前提交
d71a54ed58
共有 1 个文件被更改,包括 50 次插入0 次删除
  1. 50 0
      src/Avalonia.Base/Threading/Dispatcher.Invoke.cs

+ 50 - 0
src/Avalonia.Base/Threading/Dispatcher.Invoke.cs

@@ -540,6 +540,56 @@ public partial class Dispatcher
         InvokeAsyncImpl(new DispatcherOperation(this, priority, action, true), CancellationToken.None);
     }
 
+    /// <summary>
+    ///     Executes the specified Func<Task> asynchronously on the
+    ///     thread that the Dispatcher was created on
+    /// </summary>
+    /// <param name="callback">
+    ///     A Func<Task> delegate to invoke through the dispatcher.
+    /// </param>
+    /// <param name="priority">
+    ///     The priority that determines in what order the specified
+    ///     callback is invoked relative to the other pending operations
+    ///     in the Dispatcher.
+    /// </param>
+    /// <returns>
+    ///     An task that completes after the task returned from callback finishes
+    /// </returns>
+    /// <remarks>
+    /// DispatcherPriority is only respected for the initial call, any async continuations will be executed through
+    /// SynchronizationContext
+    /// </remarks>
+    public Task InvokeTaskAsync(Func<Task> callback, DispatcherPriority priority = default)
+    {
+        _ = callback ?? throw new ArgumentNullException(nameof(callback));
+        return InvokeAsync(callback, priority).GetTask().Unwrap();
+    }
+    
+    /// <summary>
+    ///     Executes the specified Func<Task<TResult>> asynchronously on the
+    ///     thread that the Dispatcher was created on
+    /// </summary>
+    /// <param name="callback">
+    ///     A Func<Task<TResult>> delegate to invoke through the dispatcher.
+    /// </param>
+    /// <param name="priority">
+    ///     The priority that determines in what order the specified
+    ///     callback is invoked relative to the other pending operations
+    ///     in the Dispatcher.
+    /// </param>
+    /// <returns>
+    ///     An task that completes after the task returned from callback finishes
+    /// </returns>
+    /// <remarks>
+    /// DispatcherPriority is only respected for the initial call, any async continuations will be executed through
+    /// SynchronizationContext
+    /// </remarks>
+    public Task<TResult> InvokeTaskAsync<TResult>(Func<Task<TResult>> action, DispatcherPriority priority = default)
+    {
+        _ = action ?? throw new ArgumentNullException(nameof(action));
+        return InvokeAsync(action, priority).GetTask().Unwrap();
+    }
+
     /// <summary>
     /// Posts an action that will be invoked on the dispatcher thread.
     /// </summary>