Modifica

Condividi tramite


TaskScheduler.GetScheduledTasks Method

Definition

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

protected:
 abstract System::Collections::Generic::IEnumerable<System::Threading::Tasks::Task ^> ^ GetScheduledTasks();
[System.Security.SecurityCritical]
protected abstract System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> GetScheduledTasks ();
protected abstract System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>? GetScheduledTasks ();
protected abstract System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> GetScheduledTasks ();
[<System.Security.SecurityCritical>]
abstract member GetScheduledTasks : unit -> seq<System.Threading.Tasks.Task>
abstract member GetScheduledTasks : unit -> seq<System.Threading.Tasks.Task>
Protected MustOverride Function GetScheduledTasks () As IEnumerable(Of Task)

Returns

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

Attributes

Exceptions

This scheduler is unable to generate a list of queued tasks at this time.

Remarks

A class derived from 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 block. The following example shows one possible approach in C#:

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 TaskScheduler instances. You can then use GetScheduledTasksForDebugger on each scheduler instance to retrieve its list of scheduled tasks.

Applies to

See also