TaskScheduler

Namespace: System.Threading.Tasks

Class TaskScheduler

Represents a schedule for executing tasks.

Inheritance: System.Object TaskScheduler
Description
Members

Description

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:

  • Dispatching tasks to the appropriate execution context.
  • Managing the underlying execution resources (e.g., threads).
  • Handling task priorities and concurrency levels.

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.

Remarks

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.

Public Static Properties

  • Default public static TaskScheduler Default { get; }

    Gets the default scheduler for the current thread. This scheduler is typically the ThreadPoolTaskScheduler.

Public Abstract Methods

  • QueueTask protected abstract void QueueTask(Task task)

    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.

  • TryExecuteTaskInline protected abstract bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyScheduled)

    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.

Protected Methods

  • FromCurrentSynchronizationContext protected static TaskScheduler FromCurrentSynchronizationContext()

    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.

  • GetMaximumConcurrencyLevel protected virtual int GetMaximumConcurrencyLevel()

    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.

  • NotifyTaskAssignedToTaskScheduler protected virtual void NotifyTaskAssignedToTaskScheduler(Task task)

    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.

  • OnRemovedFromTaskScheduler protected virtual void OnRemovedFromTaskScheduler(Task task)

    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.

Public Methods

  • ScheduleTask public int ScheduleTask(Func<Task> action)

    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."); });

  • FromThreadPool public static TaskScheduler FromThreadPool(ThreadPoolExecutor executor)

    Creates a TaskScheduler that uses the specified ThreadPoolExecutor.

    This method is obsolete. Use ThreadPool.QueueUserWorkItem directly.

Fields

  • MaximumConcurrencyLevel public const int MaximumConcurrencyLevel = 32767

    Represents the maximum number of concurrent tasks that may be executing on this scheduler.

See Also