| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
|
| 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 |
|
| 20 |
/// <summary> |
| 21 |
/// Returns the ISchedulerLongRunning implementation of the specified scheduler, or null if no such implementation is available. |
| 22 |
/// </summary> |
| 23 |
/// <param name="scheduler">Scheduler to get the ISchedulerLongRunning implementation for.</param> |
| 24 |
/// <returns>The scheduler's ISchedulerLongRunning implementation if available; null otherwise.</returns> |
| 25 |
/// <remarks> |
| 26 |
/// This helper method is made available for query operator authors in order to discover scheduler services by using the required |
| 27 |
/// IServiceProvider pattern, which allows for interception or redefinition of scheduler services. |
| 28 |
/// </remarks> |
| 29 |
public static ISchedulerLongRunning AsLongRunning(this IScheduler scheduler) |
| 30 |
{ |
| 31 |
var svc = scheduler as IServiceProvider; |
| 32 |
if (svc != null) |
| 33 |
return (ISchedulerLongRunning)svc.GetService(typeof(ISchedulerLongRunning)); |
| 34 |
|
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
/// <summary> |
| 39 |
/// Returns the IStopwatchProvider implementation of the specified scheduler, or null if no such implementation is available. |
| 40 |
/// </summary> |
| 41 |
/// <param name="scheduler">Scheduler to get the IStopwatchProvider implementation for.</param> |
| 42 |
/// <returns>The scheduler's IStopwatchProvider implementation if available; null otherwise.</returns> |
| 43 |
/// <remarks> |
| 44 |
/// <para> |
| 45 |
/// This helper method is made available for query operator authors in order to discover scheduler services by using the required |
| 46 |
/// IServiceProvider pattern, which allows for interception or redefinition of scheduler services. |
| 47 |
/// </para> |
| 48 |
/// <para> |
| 49 |
/// Consider using <see cref="Scheduler.StartStopwatch"/> in case a stopwatch is required, but use of emulation stopwatch based |
| 50 |
/// on the scheduler's clock is acceptable. Use of this method is recommended for best-effort use of the stopwatch provider |
| 51 |
/// scheduler service, where the caller falls back to not using stopwatches if this facility wasn't found. |
| 52 |
/// </para> |
| 53 |
/// </remarks> |
| 54 |
public static IStopwatchProvider AsStopwatchProvider(this IScheduler scheduler) |
| 55 |
{ |
| 56 |
var svc = scheduler as IServiceProvider; |
| 57 |
if (svc != null) |
| 58 |
return (IStopwatchProvider)svc.GetService(typeof(IStopwatchProvider)); |
| 59 |
|
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
/// <summary> |
| 64 |
/// Returns the IStopwatchProvider implementation of the specified scheduler, or null if no such implementation is available. |
| 65 |
/// </summary> |
| 66 |
/// <param name="scheduler">Scheduler to get the IStopwatchProvider implementation for.</param> |
| 67 |
/// <returns>The scheduler's IStopwatchProvider implementation if available; null otherwise.</returns> |
| 68 |
/// <remarks> |
| 69 |
/// <para> |
| 70 |
/// This helper method is made available for query operator authors in order to discover scheduler services by using the required |
| 71 |
/// IServiceProvider pattern, which allows for interception or redefinition of scheduler services. |
| 72 |
/// </para> |
| 73 |
/// <para> |
| 74 |
/// Consider using the Scheduler.SchedulePeriodic extension methods for IScheduler in case periodic scheduling is required and |
| 75 |
/// emulation of periodic behavior using other scheduler services is desirable. Use of this method is recommended for best-effort |
| 76 |
/// use of the periodic scheduling service, where the caller falls back to not using periodic scheduling if this facility wasn't |
| 77 |
/// found. |
| 78 |
/// </para> |
| 79 |
/// </remarks> |
| 80 |
public static ISchedulerPeriodic AsPeriodic(this IScheduler scheduler) |
| 81 |
{ |
| 82 |
var svc = scheduler as IServiceProvider; |
| 83 |
if (svc != null) |
| 84 |
return (ISchedulerPeriodic)svc.GetService(typeof(ISchedulerPeriodic)); |
| 85 |
|
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
} |