LocalScheduler.cs 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. 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. throw new ArgumentNullException(nameof(action));
  27. return Schedule(state, TimeSpan.Zero, action);
  28. }
  29. /// <summary>
  30. /// Schedules an action to be executed after dueTime.
  31. /// </summary>
  32. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  33. /// <param name="state">State passed to the action to be executed.</param>
  34. /// <param name="action">Action to be executed.</param>
  35. /// <param name="dueTime">Relative time after which to execute the action.</param>
  36. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  37. public abstract IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action);
  38. /// <summary>
  39. /// Schedules an action to be executed at dueTime.
  40. /// </summary>
  41. /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
  42. /// <param name="state">State passed to the action to be executed.</param>
  43. /// <param name="action">Action to be executed.</param>
  44. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  45. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  46. /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
  47. public virtual IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
  48. {
  49. if (action == null)
  50. throw new ArgumentNullException(nameof(action));
  51. return Enqueue(state, dueTime, action);
  52. }
  53. /// <summary>
  54. /// Starts a new stopwatch object.
  55. /// </summary>
  56. /// <returns>New stopwatch object; started at the time of the request.</returns>
  57. /// <remarks>
  58. /// Platform-specific scheduler implementations should reimplement <see cref="IStopwatchProvider"/>
  59. /// to provide a more efficient <see cref="IStopwatch"/> implementation (if available).
  60. /// </remarks>
  61. public virtual IStopwatch StartStopwatch() => ConcurrencyAbstractionLayer.Current.StartStopwatch();
  62. object IServiceProvider.GetService(Type serviceType) => GetService(serviceType);
  63. /// <summary>
  64. /// Discovers scheduler services by interface type. The base class implementation returns
  65. /// requested services for each scheduler interface implemented by the derived class. For
  66. /// more control over service discovery, derived types can override this method.
  67. /// </summary>
  68. /// <param name="serviceType">Scheduler service interface type to discover.</param>
  69. /// <returns>Object implementing the requested service, if available; <c>null</c> otherwise.</returns>
  70. protected virtual object GetService(Type serviceType)
  71. {
  72. if (serviceType == typeof(IStopwatchProvider))
  73. return this as IStopwatchProvider;
  74. else if (serviceType == typeof(ISchedulerLongRunning))
  75. return this as ISchedulerLongRunning;
  76. else if (serviceType == typeof(ISchedulerPeriodic))
  77. return this as ISchedulerPeriodic;
  78. return null;
  79. }
  80. }
  81. }