SynchronizationContextScheduler.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. if (context == null)
  23. throw new ArgumentNullException(nameof(context));
  24. _context = context;
  25. _alwaysPost = true;
  26. }
  27. /// <summary>
  28. /// Creates an object that schedules units of work on the provided <see cref="SynchronizationContext"/>.
  29. /// </summary>
  30. /// <param name="context">Synchronization context to schedule units of work on.</param>
  31. /// <param name="alwaysPost">Configures whether scheduling always posts to the synchronization context, regardless whether the caller is on the same synchronization context.</param>
  32. /// <exception cref="ArgumentNullException"><paramref name="context"/> is <c>null</c>.</exception>
  33. public SynchronizationContextScheduler(SynchronizationContext context, bool alwaysPost)
  34. {
  35. if (context == null)
  36. throw new ArgumentNullException(nameof(context));
  37. _context = context;
  38. _alwaysPost = alwaysPost;
  39. }
  40. /// <summary>
  41. /// Schedules an action to be executed.
  42. /// </summary>
  43. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  44. /// <param name="state">State passed to the action to be executed.</param>
  45. /// <param name="action">Action to be executed.</param>
  46. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  47. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  48. public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  49. {
  50. if (action == null)
  51. throw new ArgumentNullException(nameof(action));
  52. var d = new SingleAssignmentDisposable();
  53. if (!_alwaysPost && _context == SynchronizationContext.Current)
  54. {
  55. d.Disposable = action(this, state);
  56. }
  57. else
  58. {
  59. _context.PostWithStartComplete(() =>
  60. {
  61. if (!d.IsDisposed)
  62. {
  63. d.Disposable = action(this, state);
  64. }
  65. });
  66. }
  67. return d;
  68. }
  69. /// <summary>
  70. /// Schedules an action to be executed after dueTime.
  71. /// </summary>
  72. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  73. /// <param name="state">State passed to the action to be executed.</param>
  74. /// <param name="action">Action to be executed.</param>
  75. /// <param name="dueTime">Relative time after which to execute the action.</param>
  76. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  77. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  78. public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
  79. {
  80. if (action == null)
  81. throw new ArgumentNullException(nameof(action));
  82. var dt = Scheduler.Normalize(dueTime);
  83. if (dt.Ticks == 0)
  84. {
  85. return Schedule(state, action);
  86. }
  87. return DefaultScheduler.Instance.Schedule(state, dt, (_, state1) => Schedule(state1, action));
  88. }
  89. }
  90. }