소스 검색

Merge pull request #1823 from Kryptos-FR/feature/dispatcher-function

Support invoking a task in the dispatcher.
Jeremy Koritzinsky 7 년 전
부모
커밋
5048562f25
3개의 변경된 파일50개의 추가작업 그리고 1개의 파일을 삭제
  1. 14 0
      src/Avalonia.Base/Threading/Dispatcher.cs
  2. 18 0
      src/Avalonia.Base/Threading/IDispatcher.cs
  3. 18 1
      tests/Avalonia.UnitTests/ImmediateDispatcher.cs

+ 14 - 0
src/Avalonia.Base/Threading/Dispatcher.cs

@@ -92,6 +92,20 @@ namespace Avalonia.Threading
             return _jobRunner.InvokeAsync(function, priority);
         }
 
+        /// <inheritdoc/>
+        public Task InvokeAsync(Func<Task> function, DispatcherPriority priority = DispatcherPriority.Normal)
+        {
+            Contract.Requires<ArgumentNullException>(function != null);
+            return _jobRunner.InvokeAsync(function, priority).Unwrap();
+        }
+
+        /// <inheritdoc/>
+        public Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> function, DispatcherPriority priority = DispatcherPriority.Normal)
+        {
+            Contract.Requires<ArgumentNullException>(function != null);
+            return _jobRunner.InvokeAsync(function, priority).Unwrap();
+        }
+
         /// <inheritdoc/>
         public void Post(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
         {

+ 18 - 0
src/Avalonia.Base/Threading/IDispatcher.cs

@@ -40,5 +40,23 @@ namespace Avalonia.Threading
         /// <param name="function">The method.</param>
         /// <param name="priority">The priority with which to invoke the method.</param>
         Task<TResult> InvokeAsync<TResult>(Func<TResult> function, DispatcherPriority priority = DispatcherPriority.Normal);
+
+        /// <summary>
+        /// Queues the specified work to run on the dispatcher thread and returns a proxy for the
+        /// task returned by <paramref name="function"/>.
+        /// </summary>
+        /// <param name="function">The work to execute asynchronously.</param>
+        /// <param name="priority">The priority with which to invoke the method.</param>
+        /// <returns>A task that represents a proxy for the task returned by <paramref name="function"/>.</returns>
+        Task InvokeAsync(Func<Task> function, DispatcherPriority priority = DispatcherPriority.Normal);
+
+        /// <summary>
+        /// Queues the specified work to run on the dispatcher thread and returns a proxy for the
+        /// task returned by <paramref name="function"/>.
+        /// </summary>
+        /// <param name="function">The work to execute asynchronously.</param>
+        /// <param name="priority">The priority with which to invoke the method.</param>
+        /// <returns>A task that represents a proxy for the task returned by <paramref name="function"/>.</returns>
+        Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> function, DispatcherPriority priority = DispatcherPriority.Normal);
     }
 }

+ 18 - 1
tests/Avalonia.UnitTests/ImmediateDispatcher.cs

@@ -9,28 +9,45 @@ namespace Avalonia.UnitTests
     /// </summary>
     public class ImmediateDispatcher : IDispatcher
     {
+        /// <inheritdoc/>
         public bool CheckAccess()
         {
             return true;
         }
 
+        /// <inheritdoc/>
         public void Post(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
         {
             action();
         }
 
+        /// <inheritdoc/>
         public Task InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
         {
             action();
-            return Task.FromResult<object>(null);
+            return Task.CompletedTask;
         }
 
+        /// <inheritdoc/>
         public Task<TResult> InvokeAsync<TResult>(Func<TResult> function, DispatcherPriority priority = DispatcherPriority.Normal)
         {
             var result = function();
             return Task.FromResult(result);
         }
 
+        /// <inheritdoc/>
+        public Task InvokeAsync(Func<Task> function, DispatcherPriority priority = DispatcherPriority.Normal)
+        {
+            return function();
+        }
+        
+        /// <inheritdoc/>
+        public Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> function, DispatcherPriority priority = DispatcherPriority.Normal)
+        {
+            return function();
+        }
+        
+        /// <inheritdoc/>
         public void VerifyAccess()
         {
         }