SynchronizationContextScheduler.cs 4.4 KB

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