LocalScheduler.cs 4.7 KB

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