LocalScheduler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Reactive.Concurrency
  5. {
  6. /// <summary>
  7. /// Abstract base class for machine-local schedulers, using the local system clock for time-based operations.
  8. /// </summary>
  9. public abstract partial class LocalScheduler : IScheduler, IStopwatchProvider, IServiceProvider
  10. {
  11. /// <summary>
  12. /// Gets the scheduler's notion of current time.
  13. /// </summary>
  14. public virtual DateTimeOffset Now => Scheduler.Now;
  15. /// <summary>
  16. /// Schedules an action to be executed.
  17. /// </summary>
  18. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  19. /// <param name="state">State passed to the action to be executed.</param>
  20. /// <param name="action">Action to be executed.</param>
  21. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  22. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  23. public virtual IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  24. {
  25. if (action == null)
  26. {
  27. throw new ArgumentNullException(nameof(action));
  28. }
  29. return Schedule(state, TimeSpan.Zero, action);
  30. }
  31. /// <summary>
  32. /// Schedules an action to be executed after dueTime.
  33. /// </summary>
  34. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  35. /// <param name="state">State passed to the action to be executed.</param>
  36. /// <param name="action">Action to be executed.</param>
  37. /// <param name="dueTime">Relative time after which to execute the action.</param>
  38. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  39. public abstract IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action);
  40. /// <summary>
  41. /// Schedules an action to be executed at dueTime.
  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. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  47. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  48. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  49. public virtual IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
  50. {
  51. if (action == null)
  52. {
  53. throw new ArgumentNullException(nameof(action));
  54. }
  55. return Enqueue(state, dueTime, action);
  56. }
  57. /// <summary>
  58. /// Starts a new stopwatch object.
  59. /// </summary>
  60. /// <returns>New stopwatch object; started at the time of the request.</returns>
  61. /// <remarks>
  62. /// Platform-specific scheduler implementations should reimplement <see cref="IStopwatchProvider"/>
  63. /// to provide a more efficient <see cref="IStopwatch"/> implementation (if available).
  64. /// </remarks>
  65. public virtual IStopwatch StartStopwatch() => ConcurrencyAbstractionLayer.Current.StartStopwatch();
  66. object? IServiceProvider.GetService(Type serviceType) => GetService(serviceType);
  67. /// <summary>
  68. /// Discovers scheduler services by interface type. The base class implementation returns
  69. /// requested services for each scheduler interface implemented by the derived class. For
  70. /// more control over service discovery, derived types can override this method.
  71. /// </summary>
  72. /// <param name="serviceType">Scheduler service interface type to discover.</param>
  73. /// <returns>Object implementing the requested service, if available; <c>null</c> otherwise.</returns>
  74. protected virtual object? GetService(Type serviceType)
  75. {
  76. if (serviceType == typeof(IStopwatchProvider))
  77. {
  78. return this;
  79. }
  80. if (serviceType == typeof(ISchedulerLongRunning))
  81. {
  82. return this as ISchedulerLongRunning;
  83. }
  84. if (serviceType == typeof(ISchedulerPeriodic))
  85. {
  86. return this as ISchedulerPeriodic;
  87. }
  88. return null;
  89. }
  90. }
  91. }