Namespace: System.Threading.Tasks
Represents a schedule for executing tasks.
The TaskScheduler class abstracts the process of scheduling tasks to be executed. It allows for customization of how and when tasks are run, enabling scenarios like thread pooling, UI thread marshaling, and custom task dispatching. The .NET Framework provides default implementations, such as the ThreadPoolTaskScheduler
, which are typically used unless specific custom behavior is required.
Key responsibilities of a TaskScheduler
include:
When you create a new Task
or call methods like Task.Run
, a TaskScheduler
is implicitly or explicitly involved. The default scheduler is usually the ThreadPoolTaskScheduler
, which uses the .NET ThreadPool.
Custom task schedulers can be derived from this abstract class to implement specialized scheduling logic. For instance, a UI application might use a custom scheduler to ensure that tasks updating the UI are executed on the UI thread.
This class is abstract and cannot be instantiated directly. You will typically interact with instances of concrete derived classes or use the default scheduler provided by the .NET runtime.
The TaskScheduler
class is fundamental to the Task Parallel Library (TPL), providing a flexible mechanism for managing task execution.
Gets the default scheduler for the current thread. This scheduler is typically the ThreadPoolTaskScheduler.
Queues the specified task to be executed on the current scheduler.
This method must be implemented by derived classes to provide the actual task scheduling logic. It is called internally by the TPL when a task needs to be dispatched.
Attempts to execute the specified task inline on the current thread.
Derived classes can override this method to try and execute a task immediately without queuing if the current context is appropriate. For example, a UI scheduler might execute inline if it's already on the UI thread.
Creates a TaskScheduler that is associated with the current synchronization context.
This method is typically used in UI applications to create a scheduler that marshals tasks to the UI thread.
Gets the maximum concurrency level supported by this scheduler.
This is a hint to the TPL about how many tasks can be executed concurrently by this scheduler.
Notifies the scheduler that a task has been assigned to it.
This method is called internally by the TPL and is not intended for direct use.
Notifies the scheduler that a task has been removed from it.
This method is called internally by the TPL and is not intended for direct use.
Schedules the specified delegate to be executed asynchronously.
This is a convenience method that wraps the task creation and scheduling process.
Example:int taskId = scheduler.ScheduleTask(async () => { await Task.Delay(100); Console.WriteLine("Task completed."); });
Creates a TaskScheduler that uses the specified ThreadPoolExecutor.
This method is obsolete. Use ThreadPool.QueueUserWorkItem directly.
Represents the maximum number of concurrent tasks that may be executing on this scheduler.