SynchronizationContextScheduler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Disposables;
  5. using System.Threading;
  6. namespace System.Reactive.Concurrency
  7. {
  8. /// <summary>
  9. /// Represents an object that schedules units of work on a provided <seealso cref="SynchronizationContext"/>.
  10. /// </summary>
  11. public class SynchronizationContextScheduler : LocalScheduler
  12. {
  13. private readonly SynchronizationContext _context;
  14. private readonly bool _alwaysPost;
  15. /// <summary>
  16. /// Creates an object that schedules units of work on the provided <see cref="SynchronizationContext"/>.
  17. /// </summary>
  18. /// <param name="context">Synchronization context to schedule units of work on.</param>
  19. /// <exception cref="ArgumentNullException"><paramref name="context"/> is <c>null</c>.</exception>
  20. public SynchronizationContextScheduler(SynchronizationContext context)
  21. {
  22. _context = context ?? throw new ArgumentNullException(nameof(context));
  23. _alwaysPost = true;
  24. }
  25. /// <summary>
  26. /// Creates an object that schedules units of work on the provided <see cref="SynchronizationContext"/>.
  27. /// </summary>
  28. /// <param name="context">Synchronization context to schedule units of work on.</param>
  29. /// <param name="alwaysPost">Configures whether scheduling always posts to the synchronization context, regardless whether the caller is on the same synchronization context.</param>
  30. /// <exception cref="ArgumentNullException"><paramref name="context"/> is <c>null</c>.</exception>
  31. public SynchronizationContextScheduler(SynchronizationContext context, bool alwaysPost)
  32. {
  33. _context = context ?? throw new ArgumentNullException(nameof(context));
  34. _alwaysPost = alwaysPost;
  35. }
  36. /// <summary>
  37. /// Schedules an action to be executed.
  38. /// </summary>
  39. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  40. /// <param name="state">State passed to the action to be executed.</param>
  41. /// <param name="action">Action to be executed.</param>
  42. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  43. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  44. public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  45. {
  46. if (action == null)
  47. {
  48. throw new ArgumentNullException(nameof(action));
  49. }
  50. if (!_alwaysPost && _context == SynchronizationContext.Current)
  51. {
  52. return action(this, state);
  53. }
  54. var d = new SingleAssignmentDisposable();
  55. _context.PostWithStartComplete(() =>
  56. {
  57. if (!d.IsDisposed)
  58. {
  59. d.Disposable = action(this, state);
  60. }
  61. });
  62. return d;
  63. }
  64. /// <summary>
  65. /// Schedules an action to be executed after dueTime.
  66. /// </summary>
  67. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  68. /// <param name="state">State passed to the action to be executed.</param>
  69. /// <param name="action">Action to be executed.</param>
  70. /// <param name="dueTime">Relative time after which to execute the action.</param>
  71. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  72. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  73. public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
  74. {
  75. if (action == null)
  76. {
  77. throw new ArgumentNullException(nameof(action));
  78. }
  79. var dt = Scheduler.Normalize(dueTime);
  80. if (dt.Ticks == 0)
  81. {
  82. return Schedule(state, action);
  83. }
  84. return DefaultScheduler.Instance.Schedule((scheduler: this, action, state), dt, (_, tuple) => tuple.scheduler.Schedule(tuple.state, tuple.action));
  85. }
  86. }
  87. }