Scheduler.Services.cs 4.6 KB

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