MonoThreadedTaskScheduler Class

Provides a task scheduler that executes tasks on a single dedicated thread.

public sealed class MonoThreadedTaskScheduler : TaskScheduler, IDisposable

Example

using var scheduler = new MonoThreadedTaskScheduler("MyWorkerThread");
var threadName = await Task.Factory.StartNew(
    () => Thread.CurrentThread.Name,
    CancellationToken.None,
    TaskCreationOptions.None,
    scheduler);
Assert.Equal("MyWorkerThread", threadName);
Inheritance
TaskScheduler MonoThreadedTaskScheduler
Implements

Constructors

NameDescription
MonoThreadedTaskScheduler()Initializes a new instance of the MonoThreadedTaskScheduler class.
MonoThreadedTaskScheduler(string?)Initializes a new instance of the MonoThreadedTaskScheduler class with the specified thread name.

MonoThreadedTaskScheduler overloads

MonoThreadedTaskScheduler()

MonoThreadedTaskScheduler.cs

Initializes a new instance of the MonoThreadedTaskScheduler class.

public MonoThreadedTaskScheduler()

MonoThreadedTaskScheduler(string?)

MonoThreadedTaskScheduler.cs

Initializes a new instance of the MonoThreadedTaskScheduler class with the specified thread name.

public MonoThreadedTaskScheduler(string? threadName)
Parameters
threadName string?
The name of the worker thread.

Properties

NameDescription
DequeueOnDisposeGets or sets a value indicating whether to dequeue remaining tasks when the scheduler is disposed.
DisposeThreadJoinTimeoutGets or sets the timeout to wait for the worker thread to complete when disposing. The default is one second.
MaximumConcurrencyLevelGets the maximum concurrency level supported by this scheduler, which is always 1.
QueueCountGets the number of tasks currently queued to the scheduler.
WaitTimeoutGets or sets the timeout for waiting on the event handles. The default is Timeout.InfiniteTimeSpan.
WorkerExceptionGets the exception that terminated the worker thread, or null when the worker thread is still running or has stopped normally.

Gets or sets a value indicating whether to dequeue remaining tasks when the scheduler is disposed.

public bool DequeueOnDispose { get; set; }

Property value

bool

Remarks

When true, every task the scheduler accepted is executed before the worker thread exits. When false, the tasks that have not started running when MonoThreadedTaskScheduler.Dispose is called are abandoned and never complete. In both cases no task is accepted once MonoThreadedTaskScheduler.Dispose has been called.

DisposeThreadJoinTimeout

MonoThreadedTaskScheduler.cs

Gets or sets the timeout to wait for the worker thread to complete when disposing. The default is one second.

public TimeSpan DisposeThreadJoinTimeout { get; set; }

Property value

TimeSpan

Exceptions

ArgumentOutOfRangeException
The value is negative and is not Timeout.InfiniteTimeSpan, or represents more than Int32.MaxValue milliseconds.

Remarks

The wait is skipped when MonoThreadedTaskScheduler.Dispose is called from the scheduler's own thread.

MaximumConcurrencyLevel

MonoThreadedTaskScheduler.cs

Gets the maximum concurrency level supported by this scheduler, which is always 1.

public override int MaximumConcurrencyLevel { get; }

Property value

int

Gets the number of tasks currently queued to the scheduler.

public int QueueCount { get; }

Property value

int

Gets or sets the timeout for waiting on the event handles. The default is Timeout.InfiniteTimeSpan.

public TimeSpan WaitTimeout { get; set; }

Property value

TimeSpan

Exceptions

ArgumentOutOfRangeException
The value is negative and is not Timeout.InfiniteTimeSpan, or represents more than Int32.MaxValue milliseconds.

Remarks

Queued tasks and disposal both signal the worker thread, so polling is not needed to make progress. Set a finite value only to have the worker thread wake up periodically while the scheduler is idle.

Gets the exception that terminated the worker thread, or null when the worker thread is still running or has stopped normally.

public Exception? WorkerException { get; }

Property value

Exception?

Remarks

A terminated worker thread cannot be replaced without breaking the guarantee that every task runs on the same thread. The scheduler therefore stops accepting tasks once this property is not null.

Methods

NameDescription
Dispose()
public void Dispose()

No documentation is available.