MonoThreadedTaskScheduler Class
Provides a task scheduler that executes tasks on a single dedicated thread.
public sealed class MonoThreadedTaskScheduler : TaskScheduler, IDisposableExample
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
TaskSchedulerMonoThreadedTaskScheduler- Implements
Constructors
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the MonoThreadedTaskScheduler class.
public MonoThreadedTaskScheduler()MonoThreadedTaskScheduler(string?)
MonoThreadedTaskScheduler.csInitializes a new instance of the MonoThreadedTaskScheduler class with the specified thread name.
public MonoThreadedTaskScheduler(string? threadName)Parameters
threadNamestring?- The name of the worker thread.
Properties
| Name | Description |
|---|---|
| DequeueOnDispose | Gets or sets a value indicating whether to dequeue remaining tasks when the scheduler is disposed. |
| DisposeThreadJoinTimeout | Gets or sets the timeout to wait for the worker thread to complete when disposing. The default is one second. |
| MaximumConcurrencyLevel | Gets the maximum concurrency level supported by this scheduler, which is always 1. |
| QueueCount | Gets the number of tasks currently queued to the scheduler. |
| WaitTimeout | Gets or sets the timeout for waiting on the event handles. The default is Timeout.InfiniteTimeSpan. |
| WorkerException | Gets the exception that terminated the worker thread, or null when the worker thread is still running or has stopped normally. |
DequeueOnDispose
MonoThreadedTaskScheduler.csGets or sets a value indicating whether to dequeue remaining tasks when the scheduler is disposed.
public bool DequeueOnDispose { get; set; }Property value
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.csGets 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
Exceptions
ArgumentOutOfRangeException- The value is negative and is not
Timeout.InfiniteTimeSpan, or represents more thanInt32.MaxValuemilliseconds.
Remarks
The wait is skipped when MonoThreadedTaskScheduler.Dispose is called from the scheduler's own thread.
MaximumConcurrencyLevel
MonoThreadedTaskScheduler.csQueueCount
MonoThreadedTaskScheduler.csWaitTimeout
MonoThreadedTaskScheduler.csGets or sets the timeout for waiting on the event handles. The default is Timeout.InfiniteTimeSpan.
public TimeSpan WaitTimeout { get; set; }Property value
Exceptions
ArgumentOutOfRangeException- The value is negative and is not
Timeout.InfiniteTimeSpan, or represents more thanInt32.MaxValuemilliseconds.
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.
WorkerException
MonoThreadedTaskScheduler.csGets 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
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
| Name | Description |
|---|---|
| Dispose() |
Dispose()
MonoThreadedTaskScheduler.cspublic void Dispose()No documentation is available.