TaskScheduler.GetScheduledTasks 方法
定义
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)
返回
一个允许调试器遍历当前排队到此计划程序中的任务的枚举。An enumerable that allows a debugger to traverse the tasks currently queued to this scheduler.
- 属性
例外
此计划程序无法在此时生成排队任务的列表。This scheduler is unable to generate a list of queued tasks at this time.
注解
派生自的类 TaskScheduler 实现此方法以支持与调试器的集成。A class derived from TaskScheduler implements this method in order to support integration with debuggers. 仅当调试器请求对数据的访问权限时,.NET Framework 才会调用此方法。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. 下面的示例演示了 c # 中的一种可能的方法: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.
返回的可枚举不应为 null。The returned enumerable should never be null. 如果当前没有排队的任务,则应改为返回空的可枚举。If there are currently no queued tasks, an empty enumerable should be returned instead.
实现自定义调试器的开发人员不应直接调用此方法,而应改用内部包装方法 GetScheduledTasksForDebugger : internal Task[] GetScheduledTasksForDebugger() 。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. 若要检索活动计划程序的列表,请使用 internal 方法 internal static TaskScheduler[] GetTaskSchedulersForDebugger() 。To retrieve a list of active schedulers, use the internal method internal static TaskScheduler[] GetTaskSchedulersForDebugger(). 此静态方法返回所有活动实例的数组 TaskScheduler 。This static method returns an array of all active TaskScheduler instances. 然后,你可以 GetScheduledTasksForDebugger 在每个计划程序实例上使用来检索其计划任务的列表。You can then use GetScheduledTasksForDebugger on each scheduler instance to retrieve its list of scheduled tasks.