| 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 |
/// <summary> |
| 8 |
/// Abstract base class for machine-local schedulers, using the local system clock for time-based operations. |
| 9 |
/// </summary> |
| 10 |
public abstract partial class LocalScheduler : IScheduler, IStopwatchProvider, IServiceProvider |
| 11 |
{ |
| 12 |
/// <summary> |
| 13 |
/// Gets the scheduler's notion of current time. |
| 14 |
/// </summary> |
| 15 |
public virtual DateTimeOffset Now |
| 16 |
{ |
| 17 |
get { return Scheduler.Now; } |
| 18 |
} |
| 19 |
|
| 20 |
/// <summary> |
| 21 |
/// Schedules an action to be executed. |
| 22 |
/// </summary> |
| 23 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 24 |
/// <param name="state">State passed to the action to be executed.</param> |
| 25 |
/// <param name="action">Action to be executed.</param> |
| 26 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 27 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 28 |
public virtual IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 29 |
{ |
| 30 |
if (action == null) |
| 31 |
throw new ArgumentNullException("action"); |
| 32 |
|
| 33 |
return Schedule(state, TimeSpan.Zero, action); |
| 34 |
} |
| 35 |
|
| 36 |
/// <summary> |
| 37 |
/// Schedules an action to be executed after dueTime. |
| 38 |
/// </summary> |
| 39 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 40 |
/// <param name="state">State passed to the action to be executed.</param> |
| 41 |
/// <param name="action">Action to be executed.</param> |
| 42 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 43 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 44 |
public abstract IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action); |
| 45 |
|
| 46 |
/// <summary> |
| 47 |
/// Schedules an action to be executed at dueTime. |
| 48 |
/// </summary> |
| 49 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 50 |
/// <param name="state">State passed to the action to be executed.</param> |
| 51 |
/// <param name="action">Action to be executed.</param> |
| 52 |
/// <param name="dueTime">Absolute time at which to execute the action.</param> |
| 53 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 54 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 55 |
public virtual IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action) |
| 56 |
{ |
| 57 |
if (action == null) |
| 58 |
throw new ArgumentNullException("action"); |
| 59 |
|
| 60 |
return Enqueue(this, state, dueTime, action); |
| 61 |
} |
| 62 |
|
| 63 |
/// <summary> |
| 64 |
/// Starts a new stopwatch object. |
| 65 |
/// </summary> |
| 66 |
/// <returns>New stopwatch object; started at the time of the request.</returns> |
| 67 |
/// <remarks> |
| 68 |
/// Platform-specific scheduler implementations should reimplement IStopwatchProvider to provide a more |
| 69 |
/// efficient IStopwatch implementation (if available). |
| 70 |
/// </remarks> |
| 71 |
public virtual IStopwatch StartStopwatch() |
| 72 |
{ |
| 73 |
return ConcurrencyAbstractionLayer.Current.StartStopwatch(); |
| 74 |
} |
| 75 |
|
| 76 |
object IServiceProvider.GetService(Type serviceType) |
| 77 |
{ |
| 78 |
return GetService(serviceType); |
| 79 |
} |
| 80 |
|
| 81 |
/// <summary> |
| 82 |
/// Discovers scheduler services by interface type. The base class implementation returns |
| 83 |
/// requested services for each scheduler interface implemented by the derived class. For |
| 84 |
/// more control over service discovery, derived types can override this method. |
| 85 |
/// </summary> |
| 86 |
/// <param name="serviceType">Scheduler service interface type to discover.</param> |
| 87 |
/// <returns>Object implementing the requested service, if available; null otherwise.</returns> |
| 88 |
protected virtual object GetService(Type serviceType) |
| 89 |
{ |
| 90 |
#if !NO_PERF |
| 91 |
if (serviceType == typeof(IStopwatchProvider)) |
| 92 |
return this as IStopwatchProvider; |
| 93 |
else if (serviceType == typeof(ISchedulerLongRunning)) |
| 94 |
return this as ISchedulerLongRunning; |
| 95 |
else if (serviceType == typeof(ISchedulerPeriodic)) |
| 96 |
return this as ISchedulerPeriodic; |
| 97 |
#endif |
| 98 |
|
| 99 |
return null; |
| 100 |
} |
| 101 |
} |
| 102 |
} |