System.Threading.Tasks.TaskScheduler.GetScheduledTasks Method

For debugger support only, generates an enumerable of System.Threading.Tasks.Task instances currently queued to the scheduler waiting to be executed.

Syntax

protected abstract IEnumerable<Task> GetScheduledTasks ()

Returns

An enumerable that allows a debugger to traverse the tasks currently queued to this scheduler.

Remarks

A class derived from System.Threading.Tasks.TaskScheduler implements this method in order to support integration with debuggers. This method will only be invoked by the .NET Framework when the debugger requests access to the data. The enumerable returned will be traversed by debugging utilities to access the tasks currently queued to this scheduler, enabling the debugger to provide a representation of this information in the user interface.

It is important to note that, when this method is called, all other threads in the process will be frozen. Therefore, it's important to avoid synchronization with other threads that may lead to blocking. If synchronization is necessary, and you are unable to acquire the lock in this method, then you should throw an exception so that the debugger does not hang. The following example shows one possible approach in C#:

Example

protected override IEnumerable<Task> GetScheduledTasks()
{
    bool lockTaken = false;
    try
    {
        Monitor.TryEnter(_syncObj, ref lockTaken);
        if (lockTaken)
        {
            return _tasks.ToArray();
        }
        else throw new NotSupportedException();
    }
    finally
    {
    if (lockTaken) Monitor.Exit(_syncObj);
    }}

Additionally, this method and the enumerable returned must not modify any globally visible state.

The returned enumerable should never be null. If there are currently no queued tasks, an empty enumerable should be returned instead.

Developers who are implementing custom debuggers shouldn't call this method directly, but should use the internal wrapper method GetScheduledTasksForDebugger instead: internal Task[] GetScheduledTasksForDebugger(). This wrapper method returns an array of tasks instead of an enumerable. To retrieve a list of active schedulers, use the internal method internal static TaskScheduler[] GetTaskSchedulersForDebugger(). This static method returns an array of all active System.Threading.Tasks.TaskScheduler instances. You can then use GetScheduledTasksForDebugger on each scheduler instance to retrieve its list of scheduled tasks.

Requirements

Namespace: System.Threading.Tasks
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 4.0.0.0