// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
namespace System.Reactive.Concurrency
{
///
/// Abstract base class for machine-local schedulers, using the local system clock for time-based operations.
///
public abstract partial class LocalScheduler : IScheduler, IStopwatchProvider, IServiceProvider
{
///
/// Gets the scheduler's notion of current time.
///
public virtual DateTimeOffset Now
{
get { return Scheduler.Now; }
}
///
/// Schedules an action to be executed.
///
/// The type of the state passed to the scheduled action.
/// State passed to the action to be executed.
/// Action to be executed.
/// The disposable object used to cancel the scheduled action (best effort).
/// is null.
public virtual IDisposable Schedule(TState state, Func action)
{
if (action == null)
throw new ArgumentNullException("action");
return Schedule(state, TimeSpan.Zero, action);
}
///
/// Schedules an action to be executed after dueTime.
///
/// The type of the state passed to the scheduled action.
/// State passed to the action to be executed.
/// Action to be executed.
/// Relative time after which to execute the action.
/// The disposable object used to cancel the scheduled action (best effort).
public abstract IDisposable Schedule(TState state, TimeSpan dueTime, Func action);
///
/// Schedules an action to be executed at dueTime.
///
/// The type of the state passed to the scheduled action.
/// State passed to the action to be executed.
/// Action to be executed.
/// Absolute time at which to execute the action.
/// The disposable object used to cancel the scheduled action (best effort).
/// is null.
public virtual IDisposable Schedule(TState state, DateTimeOffset dueTime, Func action)
{
if (action == null)
throw new ArgumentNullException("action");
return Enqueue(state, dueTime, action);
}
///
/// Starts a new stopwatch object.
///
/// New stopwatch object; started at the time of the request.
///
/// Platform-specific scheduler implementations should reimplement IStopwatchProvider to provide a more
/// efficient IStopwatch implementation (if available).
///
public virtual IStopwatch StartStopwatch()
{
return ConcurrencyAbstractionLayer.Current.StartStopwatch();
}
object IServiceProvider.GetService(Type serviceType)
{
return GetService(serviceType);
}
///
/// Discovers scheduler services by interface type. The base class implementation returns
/// requested services for each scheduler interface implemented by the derived class. For
/// more control over service discovery, derived types can override this method.
///
/// Scheduler service interface type to discover.
/// Object implementing the requested service, if available; null otherwise.
protected virtual object GetService(Type serviceType)
{
#if !NO_PERF
if (serviceType == typeof(IStopwatchProvider))
return this as IStopwatchProvider;
else if (serviceType == typeof(ISchedulerLongRunning))
return this as ISchedulerLongRunning;
else if (serviceType == typeof(ISchedulerPeriodic))
return this as ISchedulerPeriodic;
#endif
return null;
}
}
}