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