Browse Source

Merge pull request #933 from AvaloniaUI/port-idispatcher

Ported IDispatcher from scenegraph branch.
pull/928/merge
Steven Kirk 9 years ago
committed by GitHub
parent
commit
45e97df245
  1. 15
      src/Avalonia.Base/Threading/Dispatcher.cs
  2. 39
      src/Avalonia.Base/Threading/IDispatcher.cs

15
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 - /// 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. /// the one for the UI thread, retrieved via the <see cref="UIThread"/> property.
/// </remarks> /// </remarks>
public class Dispatcher public class Dispatcher : IDispatcher
{ {
private readonly JobRunner _jobRunner; private readonly JobRunner _jobRunner;
private IPlatformThreadingInterface _platform; private IPlatformThreadingInterface _platform;
@ -72,22 +72,13 @@ namespace Avalonia.Threading
_jobRunner?.RunJobs(); _jobRunner?.RunJobs();
} }
/// <summary> /// <inheritdoc/>
/// 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>
public Task InvokeTaskAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal) public Task InvokeTaskAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
{ {
return _jobRunner?.InvokeAsync(action, priority); return _jobRunner?.InvokeAsync(action, priority);
} }
/// <summary> /// <inheritdoc/>
/// 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>
public void InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal) public void InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal)
{ {
_jobRunner?.Post(action, priority); _jobRunner?.Post(action, priority);

39
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);
}
}
Loading…
Cancel
Save