Show / Hide Table of Contents

Class TaskSchedulerExtensions

Represents timer-based scheduler.

Inheritance
object
TaskSchedulerExtensions
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Threading.Tasks
Assembly: DotNext.Threading.dll
Syntax
public static class TaskSchedulerExtensions

Methods

View Source

ScheduleAsync<TArgs>(Func<TArgs, CancellationToken, ValueTask>, TArgs, TimeSpan, CancellationToken)

Schedules the specific action to be executed once after the specified delay.

Declaration
public static DelayedTask ScheduleAsync<TArgs>(Func<TArgs, CancellationToken, ValueTask> callback, TArgs args, TimeSpan delay, CancellationToken token = default)
Parameters
Type Name Description
Func<TArgs, CancellationToken, ValueTask> callback

The callback to be executed after the specified delay.

TArgs args

The arguments to be passed to the callback.

TimeSpan delay

The amount of time used to delay callback execution.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
DelayedTask

The task representing delayed execution.

Type Parameters
Name Description
TArgs

The type of arguments to be passed to the callback.

Exceptions
Type Condition
ArgumentNullException

callback is null.

ArgumentOutOfRangeException

delay is less than Zero and not equal to InfiniteTimeSpan.

View Source

ScheduleAsync<TArgs, TResult>(Func<TArgs, CancellationToken, ValueTask<TResult>>, TArgs, TimeSpan, CancellationToken)

Schedules the specific action to be executed once after the specified delay.

Declaration
public static DelayedTask<TResult> ScheduleAsync<TArgs, TResult>(Func<TArgs, CancellationToken, ValueTask<TResult>> callback, TArgs args, TimeSpan delay, CancellationToken token = default)
Parameters
Type Name Description
Func<TArgs, CancellationToken, ValueTask<TResult>> callback

The callback to be executed after the specified delay.

TArgs args

The arguments to be passed to the callback.

TimeSpan delay

The amount of time used to delay callback execution.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
DelayedTask<TResult>

The task representing delayed execution.

Type Parameters
Name Description
TArgs

The type of arguments to be passed to the callback.

TResult

The type of the result to returned from the callback.

Exceptions
Type Condition
ArgumentNullException

callback is null.

ArgumentOutOfRangeException

delay is less than Zero and not equal to InfiniteTimeSpan.

View Source

SwitchTo(TaskScheduler, bool)

Switches the asynchronous method to the specified scheduler.

Declaration
public static TaskSchedulerAwaitable SwitchTo(this TaskScheduler scheduler, bool forceYield = false)
Parameters
Type Name Description
TaskScheduler scheduler
bool forceYield

true to yield the control to the caller even if the caller is already running on the target scheduler; otherwise, false.

Returns
Type Description
TaskSchedulerAwaitable

The awaitable object that resumes the caller on the target scheduler.

Remarks

The code after await scheduler.SwitchTo() and all subsequent continuations of the asynchronous method are executed by scheduler, unless the method awaits with ConfigureAwait(false) or switches to another scheduler. If the caller is already running on the target scheduler, and forceYield is false, the method continues synchronously.
The switch inside try block affects catch and finally blocks, and the switch inside finally block affects the code after it.
The switch is not a synchronization mechanism. For instance, switching to ExclusiveScheduler doesn't switch the method back when it completes. Thus, the continuation of the caller might be executed by the exclusive scheduler as well.

Exceptions
Type Condition
ArgumentNullException

The scheduler is null.

See Also
SwitchToAsync(TaskScheduler)
View Source

SwitchToAsync(TaskScheduler)

Switches the asynchronous method to the specified scheduler, and switches it back to the original scheduler at the end of the scope.

Declaration
public static ScopedTaskSchedulerAwaitable SwitchToAsync(this TaskScheduler scheduler)
Parameters
Type Name Description
TaskScheduler scheduler
Returns
Type Description
ScopedTaskSchedulerAwaitable

The awaitable object that resumes the caller on the target scheduler and returns the scope.

Remarks

The returned scope must be used with await using statement:

await using (await scheduler.SwitchToAsync())
{
    // executed by the scheduler
}

// executed by the original scheduler

The original scheduler is restored even if the scope is left with the exception. Thus, catch and finally blocks outside the scope are executed by the original scheduler. If the caller has a synchronization context, it is restored instead of the scheduler.
The code inside the scope that awaits with ConfigureAwait(false) leaves the scheduler.

Exceptions
Type Condition
ArgumentNullException

The scheduler is null.

  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾