ImmediateDispatcher.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Avalonia.Threading;
  5. namespace Avalonia.UnitTests
  6. {
  7. /// <summary>
  8. /// Immediately invokes dispatched jobs on the current thread.
  9. /// </summary>
  10. public class ImmediateDispatcher : IDispatcher
  11. {
  12. /// <inheritdoc/>
  13. public bool CheckAccess()
  14. {
  15. return true;
  16. }
  17. /// <inheritdoc/>
  18. public void Post(Action action, DispatcherPriority priority)
  19. {
  20. action();
  21. }
  22. /// <inheritdoc/>
  23. public void Post(SendOrPostCallback action, object arg, DispatcherPriority priority)
  24. {
  25. action(arg);
  26. }
  27. /// <inheritdoc/>
  28. public Task InvokeAsync(Action action, DispatcherPriority priority)
  29. {
  30. action();
  31. return Task.CompletedTask;
  32. }
  33. /// <inheritdoc/>
  34. public Task<TResult> InvokeAsync<TResult>(Func<TResult> function, DispatcherPriority priority)
  35. {
  36. var result = function();
  37. return Task.FromResult(result);
  38. }
  39. /// <inheritdoc/>
  40. public Task InvokeAsync(Func<Task> function, DispatcherPriority priority)
  41. {
  42. return function();
  43. }
  44. /// <inheritdoc/>
  45. public Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> function, DispatcherPriority priority)
  46. {
  47. return function();
  48. }
  49. /// <inheritdoc/>
  50. public void VerifyAccess()
  51. {
  52. }
  53. }
  54. }