Scheduler.Services.cs 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. //
  7. // NOTE: When adding interface-based optimizations here, ensure to add the type to the list of
  8. // interface-based optimizations used by DisableOptimizations and the RawScheduler type.
  9. //
  10. public static partial class Scheduler
  11. {
  12. internal static Type[] OPTIMIZATIONS = new Type[] {
  13. typeof(ISchedulerLongRunning),
  14. typeof(IStopwatchProvider),
  15. typeof(ISchedulerPeriodic),
  16. /* update this list if new interface-based optimizations are added */
  17. };
  18. /// <summary>
  19. /// Returns the ISchedulerLongRunning implementation of the specified scheduler, or null if no such implementation is available.
  20. /// </summary>
  21. /// <param name="scheduler">Scheduler to get the ISchedulerLongRunning implementation for.</param>
  22. /// <returns>The scheduler's ISchedulerLongRunning implementation if available; null otherwise.</returns>
  23. /// <remarks>
  24. /// This helper method is made available for query operator authors in order to discover scheduler services by using the required
  25. /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
  26. /// </remarks>
  27. public static ISchedulerLongRunning AsLongRunning(this IScheduler scheduler)
  28. {
  29. var svc = scheduler as IServiceProvider;
  30. if (svc != null)
  31. return (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning));
  32. return null;
  33. }
  34. /// <summary>
  35. /// Returns the IStopwatchProvider implementation of the specified scheduler, or null if no such implementation is available.
  36. /// </summary>
  37. /// <param name="scheduler">Scheduler to get the IStopwatchProvider implementation for.</param>
  38. /// <returns>The scheduler's IStopwatchProvider implementation if available; null otherwise.</returns>
  39. /// <remarks>
  40. /// <para>
  41. /// This helper method is made available for query operator authors in order to discover scheduler services by using the required
  42. /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
  43. /// </para>
  44. /// <para>
  45. /// Consider using <see cref="Scheduler.StartStopwatch"/> in case a stopwatch is required, but use of emulation stopwatch based
  46. /// on the scheduler's clock is acceptable. Use of this method is recommended for best-effort use of the stopwatch provider
  47. /// scheduler service, where the caller falls back to not using stopwatches if this facility wasn't found.
  48. /// </para>
  49. /// </remarks>
  50. public static IStopwatchProvider AsStopwatchProvider(this IScheduler scheduler)
  51. {
  52. var svc = scheduler as IServiceProvider;
  53. if (svc != null)
  54. return (IStopwatchProvider)svc.GetService(typeof(IStopwatchProvider));
  55. return null;
  56. }
  57. /// <summary>
  58. /// Returns the IStopwatchProvider implementation of the specified scheduler, or null if no such implementation is available.
  59. /// </summary>
  60. /// <param name="scheduler">Scheduler to get the IStopwatchProvider implementation for.</param>
  61. /// <returns>The scheduler's IStopwatchProvider implementation if available; null otherwise.</returns>
  62. /// <remarks>
  63. /// <para>
  64. /// This helper method is made available for query operator authors in order to discover scheduler services by using the required
  65. /// IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
  66. /// </para>
  67. /// <para>
  68. /// Consider using the Scheduler.SchedulePeriodic extension methods for IScheduler in case periodic scheduling is required and
  69. /// emulation of periodic behavior using other scheduler services is desirable. Use of this method is recommended for best-effort
  70. /// use of the periodic scheduling service, where the caller falls back to not using periodic scheduling if this facility wasn't
  71. /// found.
  72. /// </para>
  73. /// </remarks>
  74. public static ISchedulerPeriodic AsPeriodic(this IScheduler scheduler)
  75. {
  76. var svc = scheduler as IServiceProvider;
  77. if (svc != null)
  78. return (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic));
  79. return null;
  80. }
  81. }
  82. }