Browse Source

Ported IDispatcher from scenegraph branch.

Steven Kirk 8 years ago
parent
commit
a56ca5f319
2 changed files with 42 additions and 12 deletions
  1. 3 12
      src/Avalonia.Base/Threading/Dispatcher.cs
  2. 39 0
      src/Avalonia.Base/Threading/IDispatcher.cs

+ 3 - 12
src/Avalonia.Base/Threading/Dispatcher.cs

@@ -15,7 +15,7 @@ namespace Avalonia.Threading
     /// In Avalonia, there is usually only a single <see cref="Dispatcher"/> in the application -
     /// the one for the UI thread, retrieved via the <see cref="UIThread"/> property.
     /// </remarks>
-    public class Dispatcher
+    public class Dispatcher : IDispatcher
     {
         private readonly JobRunner _jobRunner;
         private IPlatformThreadingInterface _platform;
@@ -72,22 +72,13 @@ namespace Avalonia.Threading
             _jobRunner?.RunJobs();
         }
 
-        /// <summary>
-        /// Invokes a method on the dispatcher thread.
-        /// </summary>
-        /// <param name="action">The method.</param>
-        /// <param name="priority">The priority with which to invoke the method.</param>
-        /// <returns>A task that can be used to track the method's execution.</returns>
+        /// <inheritdoc/>
         public Task InvokeTaskAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
         {
             return _jobRunner?.InvokeAsync(action, priority);
         }
 
-        /// <summary>
-        /// Post action that will be invoked on main thread
-        /// </summary>
-        /// <param name="action">The method.</param>
-        /// <param name="priority">The priority with which to invoke the method.</param>
+        /// <inheritdoc/>
         public void InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
         {
             _jobRunner?.Post(action, priority);

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

@@ -0,0 +1,39 @@
+using System;
+using System.Threading.Tasks;
+
+namespace Avalonia.Threading
+{
+    /// <summary>
+    /// Dispatches jobs to a thread.
+    /// </summary>
+    public interface IDispatcher
+    {
+        /// <summary>
+        /// Determines whether the calling thread is the thread associated with this <see cref="IDispatcher"/>.
+        /// </summary>
+        /// <returns>True if he calling thread is the thread associated with the dispatched, otherwise false.</returns>
+        bool CheckAccess();
+
+        /// <summary>
+        /// Throws an exception if the calling thread is not the thread associated with this <see cref="IDispatcher"/>.
+        /// </summary>
+        void VerifyAccess();
+
+        /// <summary>
+        /// Invokes a method on the dispatcher thread.
+        /// </summary>
+        /// <param name="action">The method.</param>
+        /// <param name="priority">The priority with which to invoke the method.</param>
+        /// <returns>A task that can be used to track the method's execution.</returns>
+        void InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal);
+
+        /// <summary>
+        /// Post action that will be invoked on main thread
+        /// </summary>
+        /// <param name="action">The method.</param>
+        /// <param name="priority">The priority with which to invoke the method.</param>
+        // TODO: The naming of this method is confusing: the Async suffix usually means return a task.
+        // Remove this and rename InvokeTaskAsync as InvokeAsync. See #816.
+        Task InvokeTaskAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal);
+    }
+}