Class Scheduler
Provides a set of static properties to access commonly used schedulers.
Inherited Members
Namespace: System.Reactive.Concurrency
Assembly: System.Reactive.Core.dll
Syntax
public static class Scheduler
Properties
| Improve this Doc View SourceCurrentThread
Gets a scheduler that schedules work as soon as possible on the current thread.
Declaration
public static CurrentThreadScheduler CurrentThread { get; }
Property Value
| Type | Description |
|---|---|
| CurrentThreadScheduler |
Default
Gets a scheduler that schedules work on the platform's default scheduler.
Declaration
public static DefaultScheduler Default { get; }
Property Value
| Type | Description |
|---|---|
| DefaultScheduler |
Immediate
Gets a scheduler that schedules work immediately on the current thread.
Declaration
public static ImmediateScheduler Immediate { get; }
Property Value
| Type | Description |
|---|---|
| ImmediateScheduler |
NewThread
Gets a scheduler that schedules work on a new thread using default thread creation options.
Declaration
[Obsolete("This property is no longer supported due to refactoring of the API surface and elimination of platform-specific dependencies. Please add a reference to the System.Reactive.PlatformServices assembly for your target platform and use NewThreadScheduler.Default to obtain an instance of this scheduler type. See http://go.microsoft.com/fwlink/?LinkID=260866 for more information.")]
public static IScheduler NewThread { get; }
Property Value
| Type | Description |
|---|---|
| IScheduler |
Now
Gets the current time according to the local machine's system clock.
Declaration
public static DateTimeOffset Now { get; }
Property Value
| Type | Description |
|---|---|
| DateTimeOffset |
TaskPool
Gets a scheduler that schedules work on Task Parallel Library (TPL) task pool using the default TaskScheduler.
Declaration
[Obsolete("This property is no longer supported due to refactoring of the API surface and elimination of platform-specific dependencies. Please add a reference to the System.Reactive.PlatformServices assembly for your target platform and use TaskPoolScheduler.Default to obtain an instance of this scheduler type. See http://go.microsoft.com/fwlink/?LinkID=260866 for more information.")]
public static IScheduler TaskPool { get; }
Property Value
| Type | Description |
|---|---|
| IScheduler |
ThreadPool
Gets a scheduler that schedules work on the thread pool.
Declaration
[Obsolete("This property is no longer supported due to refactoring of the API surface and elimination of platform-specific dependencies. Consider using Scheduler.Default to obtain the platform's most appropriate pool-based scheduler. In order to access a specific pool-based scheduler, please add a reference to the System.Reactive.PlatformServices assembly for your target platform and use the appropriate scheduler in the System.Reactive.Concurrency namespace. See http://go.microsoft.com/fwlink/?LinkID=260866 for more information.")]
public static IScheduler ThreadPool { get; }
Property Value
| Type | Description |
|---|---|
| IScheduler |
Methods
| Improve this Doc View SourceAsLongRunning(IScheduler)
Returns the ISchedulerLongRunning implementation of the specified scheduler, or null if no such implementation is available.
Declaration
public static ISchedulerLongRunning AsLongRunning(this IScheduler scheduler)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to get the ISchedulerLongRunning implementation for. |
Returns
| Type | Description |
|---|---|
| ISchedulerLongRunning | The scheduler's ISchedulerLongRunning implementation if available; null otherwise. |
Remarks
This helper method is made available for query operator authors in order to discover scheduler services by using the required IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
AsPeriodic(IScheduler)
Returns the IStopwatchProvider implementation of the specified scheduler, or null if no such implementation is available.
Declaration
public static ISchedulerPeriodic AsPeriodic(this IScheduler scheduler)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to get the IStopwatchProvider implementation for. |
Returns
| Type | Description |
|---|---|
| ISchedulerPeriodic | The scheduler's IStopwatchProvider implementation if available; null otherwise. |
Remarks
This helper method is made available for query operator authors in order to discover scheduler services by using the required IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
Consider using the Scheduler.SchedulePeriodic extension methods for IScheduler in case periodic scheduling is required and emulation of periodic behavior using other scheduler services is desirable. Use of this method is recommended for best-effort use of the periodic scheduling service, where the caller falls back to not using periodic scheduling if this facility wasn't found.
AsStopwatchProvider(IScheduler)
Returns the IStopwatchProvider implementation of the specified scheduler, or null if no such implementation is available.
Declaration
public static IStopwatchProvider AsStopwatchProvider(this IScheduler scheduler)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to get the IStopwatchProvider implementation for. |
Returns
| Type | Description |
|---|---|
| IStopwatchProvider | The scheduler's IStopwatchProvider implementation if available; null otherwise. |
Remarks
This helper method is made available for query operator authors in order to discover scheduler services by using the required IServiceProvider pattern, which allows for interception or redefinition of scheduler services.
Consider using StartStopwatch(IScheduler) in case a stopwatch is required, but use of emulation stopwatch based on the scheduler's clock is acceptable. Use of this method is recommended for best-effort use of the stopwatch provider scheduler service, where the caller falls back to not using stopwatches if this facility wasn't found.
Catch<TException>(IScheduler, Func<TException, Boolean>)
Returns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions.
Declaration
public static IScheduler Catch<TException>(this IScheduler scheduler, Func<TException, bool> handler)
where TException : Exception
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to apply an exception filter for. |
| Func<TException, Boolean> | handler | Handler that's run if an exception is caught. The exception will be rethrown if the handler returns false. |
Returns
| Type | Description |
|---|---|
| IScheduler | Wrapper around the original scheduler, enforcing exception handling. |
Type Parameters
| Name | Description |
|---|---|
| TException | Type of the exception to check for. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
DisableOptimizations(IScheduler)
Returns a scheduler that represents the original scheduler, without any of its interface-based optimizations (e.g. long running scheduling).
Declaration
public static IScheduler DisableOptimizations(this IScheduler scheduler)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to disable all optimizations for. |
Returns
| Type | Description |
|---|---|
| IScheduler | Proxy to the original scheduler but without any optimizations enabled. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
DisableOptimizations(IScheduler, Type[])
Returns a scheduler that represents the original scheduler, without the specified set of interface-based optimizations (e.g. long running scheduling).
Declaration
public static IScheduler DisableOptimizations(this IScheduler scheduler, params Type[] optimizationInterfaces)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to disable the specified optimizations for. |
| Type[] | optimizationInterfaces | Types of the optimization interfaces that have to be disabled. |
Returns
| Type | Description |
|---|---|
| IScheduler | Proxy to the original scheduler but without the specified optimizations enabled. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Normalize(TimeSpan)
Normalizes the specified TimeSpan value to a positive value.
Declaration
public static TimeSpan Normalize(TimeSpan timeSpan)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeSpan | The TimeSpan value to normalize. |
Returns
| Type | Description |
|---|---|
| TimeSpan | The specified TimeSpan value if it is zero or positive; otherwise, TimeSpan.Zero. |
Schedule(IScheduler, Action)
Schedules an action to be executed.
Declaration
public static IDisposable Schedule(this IScheduler scheduler, Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the action on. |
| Action | action | Action to execute. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule(IScheduler, Action<Action>)
Schedules an action to be executed recursively.
Declaration
public static IDisposable Schedule(this IScheduler scheduler, Action<Action> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the recursive action on. |
| Action<Action> | action | Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule(IScheduler, DateTimeOffset, Action)
Schedules an action to be executed at the specified absolute due time.
Declaration
public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the action on. |
| DateTimeOffset | dueTime | Absolute time at which to execute the action. |
| Action | action | Action to execute. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule(IScheduler, DateTimeOffset, Action<Action<DateTimeOffset>>)
Schedules an action to be executed recursively at a specified absolute due time.
Declaration
public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action<Action<DateTimeOffset>> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the recursive action on. |
| DateTimeOffset | dueTime | Absolute time at which to execute the action for the first time. |
| Action<Action<DateTimeOffset>> | action | Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified absolute time. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule(IScheduler, TimeSpan, Action)
Schedules an action to be executed after the specified relative due time.
Declaration
public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the action on. |
| TimeSpan | dueTime | Relative time after which to execute the action. |
| Action | action | Action to execute. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule(IScheduler, TimeSpan, Action<Action<TimeSpan>>)
Schedules an action to be executed recursively after a specified relative due time.
Declaration
public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action<Action<TimeSpan>> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the recursive action on. |
| TimeSpan | dueTime | Relative time after which to execute the action for the first time. |
| Action<Action<TimeSpan>> | action | Action to execute recursively. The parameter passed to the action is used to trigger recursive scheduling of the action at the specified relative time. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule<TState>(IScheduler, TState, Action<TState, Action<TState>>)
Schedules an action to be executed recursively.
Declaration
public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, Action<TState, Action<TState>> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the recursive action on. |
| TState | state | State passed to the action to be executed. |
| Action<TState, Action<TState>> | action | Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in recursive invocation state. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state passed to the scheduled action. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule<TState>(IScheduler, TState, DateTimeOffset, Action<TState, Action<TState, DateTimeOffset>>)
Schedules an action to be executed recursively at a specified absolute due time.
Declaration
public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action<TState, Action<TState, DateTimeOffset>> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the recursive action on. |
| TState | state | State passed to the action to be executed. |
| DateTimeOffset | dueTime | Absolute time at which to execute the action for the first time. |
| Action<TState, Action<TState, DateTimeOffset>> | action | Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state passed to the scheduled action. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Schedule<TState>(IScheduler, TState, TimeSpan, Action<TState, Action<TState, TimeSpan>>)
Schedules an action to be executed recursively after a specified relative due time.
Declaration
public static IDisposable Schedule<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Action<TState, Action<TState, TimeSpan>> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the recursive action on. |
| TState | state | State passed to the action to be executed. |
| TimeSpan | dueTime | Relative time after which to execute the action for the first time. |
| Action<TState, Action<TState, TimeSpan>> | action | Action to execute recursively. The last parameter passed to the action is used to trigger recursive scheduling of the action, passing in the recursive due time and invocation state. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state passed to the scheduled action. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
ScheduleLongRunning(ISchedulerLongRunning, Action<ICancelable>)
Schedules an action to be executed.
Declaration
public static IDisposable ScheduleLongRunning(this ISchedulerLongRunning scheduler, Action<ICancelable> action)
Parameters
| Type | Name | Description |
|---|---|---|
| ISchedulerLongRunning | scheduler | Scheduler to execute the action on. |
| Action<ICancelable> | action | Action to execute. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
SchedulePeriodic(IScheduler, TimeSpan, Action)
Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. If the scheduler supports periodic scheduling, the request will be forwarded to the periodic scheduling implementation. If the scheduler provides stopwatch functionality, the periodic task will be emulated using recursive scheduling with a stopwatch to correct for time slippage. Otherwise, the periodic task will be emulated using recursive scheduling.
Declaration
public static IDisposable SchedulePeriodic(this IScheduler scheduler, TimeSpan period, Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the action on. |
| TimeSpan | period | Period for running the work periodically. |
| Action | action | Action to be executed. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled recurring action (best effort). |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
SchedulePeriodic<TState>(IScheduler, TState, TimeSpan, Action<TState>)
Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. If the scheduler supports periodic scheduling, the request will be forwarded to the periodic scheduling implementation. If the scheduler provides stopwatch functionality, the periodic task will be emulated using recursive scheduling with a stopwatch to correct for time slippage. Otherwise, the periodic task will be emulated using recursive scheduling.
Declaration
public static IDisposable SchedulePeriodic<TState>(this IScheduler scheduler, TState state, TimeSpan period, Action<TState> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to execute the action on. |
| TState | state | State passed to the action to be executed. |
| TimeSpan | period | Period for running the work periodically. |
| Action<TState> | action | Action to be executed. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled recurring action (best effort). |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state passed to the scheduled action. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
SchedulePeriodic<TState>(IScheduler, TState, TimeSpan, Func<TState, TState>)
Schedules a periodic piece of work by dynamically discovering the scheduler's capabilities. If the scheduler supports periodic scheduling, the request will be forwarded to the periodic scheduling implementation. If the scheduler provides stopwatch functionality, the periodic task will be emulated using recursive scheduling with a stopwatch to correct for time slippage. Otherwise, the periodic task will be emulated using recursive scheduling.
Declaration
public static IDisposable SchedulePeriodic<TState>(this IScheduler scheduler, TState state, TimeSpan period, Func<TState, TState> action)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | The scheduler to run periodic work on. |
| TState | state | Initial state passed to the action upon the first iteration. |
| TimeSpan | period | Period for running the work periodically. |
| Func<TState, TState> | action | Action to be executed, potentially updating the state. |
Returns
| Type | Description |
|---|---|
| IDisposable | The disposable object used to cancel the scheduled recurring action (best effort). |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state passed to the scheduled action. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
StartStopwatch(IScheduler)
Starts a new stopwatch object by dynamically discovering the scheduler's capabilities. If the scheduler provides stopwatch functionality, the request will be forwarded to the stopwatch provider implementation. Otherwise, the stopwatch will be emulated using the scheduler's notion of absolute time.
Declaration
public static IStopwatch StartStopwatch(this IScheduler scheduler)
Parameters
| Type | Name | Description |
|---|---|---|
| IScheduler | scheduler | Scheduler to obtain a stopwatch for. |
Returns
| Type | Description |
|---|---|
| IStopwatch | New stopwatch object; started at the time of the request. |
Remarks
The resulting stopwatch object can have non-monotonic behavior.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|