使用“任务”窗口(C#、Visual Basic、C++)Using the Tasks Window (C#, Visual Basic, C++)

“任务”窗口与“线程”窗口类似,但它显示的是有关 System.Threading.Tasks.Tasktask_handleWinJS.Promise 对象(而不是各个线程)的信息。The Tasks window resembles the Threads window, except that it shows information about System.Threading.Tasks.Task, task_handle, or WinJS.Promise objects instead of each thread. 与线程一样,任务表示可并行运行的异步操作;但是,多个任务可以在同一个线程上运行。Like threads, tasks represent asynchronous operations that can run concurrently; however, multiple tasks may run on the same thread.

在托管代码中,当使用 System.Threading.Tasks.Task 对象或 await 和 async 关键字(在 Visual Basic 中为 Await 和 Async)时,可使用“任务”窗口。In managed code, you can use the Tasks window when you work with System.Threading.Tasks.Task objects or with the await and async keywords (Await and Async in VisualBasic). 有关托管代码中的任务的详细信息,请参阅并行编程For more information about tasks in managed code, see Parallel Programming.

在本机代码中,当使用任务组并行算法异步代理轻量级任务时,可使用“任务”窗口。In native code, you can use the Tasks window when you work with task groups, parallel algorithms, asynchronous agents, and lightweight tasks. 有关本机代码中的任务的详细信息,请参阅并发运行时For more information about tasks in native code, see Concurrency Runtime.

在 JavaScript 中,当使用 promise .then 代码时,可使用“任务”窗口。In JavaScript, you can use the Tasks window when you are working with promise .then code. 有关详细信息,请参阅 JavaScript 中的异步编程(UWP 应用)See Asynchronous programming in JavaScript (UWP apps) for more information.

每次中断调试器时都可以使用“任务”窗口。You can use the Tasks window whenever you break into the debugger. 通过单击“窗口”,然后单击“任务”,可以在“调试”菜单上访问该窗口。You can access it on the Debug menu by clicking Windows and then clicking Tasks. 下图显示了处于默认模式的“任务”窗口。The following illustration shows the Tasks window in its default mode.

“任务”窗口Tasks window

备注

在托管代码中,当托管线程处于休眠或联接状态时,状态为 TaskStatus.CreatedTaskStatus.WaitingForActivationTaskStatus.WaitingToRunTask 可能不会在“任务”窗口中显示。In managed code, a Task that has a status of TaskStatus.Created, TaskStatus.WaitingForActivation, or TaskStatus.WaitingToRun might not be displayed in the Tasks window when managed threads are in a sleep or join state.

任务列信息Tasks Column Information

“任务”窗口中的列显示了以下信息。The columns in the Tasks window show the following information.

列名Column Name 描述Description
标记Flags 显示哪些任务已被标记,并且你可以标记或取消标记任务。Shows which tasks are flagged and lets you flag or unflag a task.
图标Icons 黄色箭头指示当前任务。A yellow arrow indicates the current task. 当前任务是当前线程上处于最顶层的任务。The current task is the top-most task on the current thread.

白色箭头指示中断的任务,即调用调试器时的任务。A white arrow indicates the breaking task, that is, the one that was current when the debugger was invoked.

暂停图标指示已被用户冻结的任务。The pause icon indicates a task that has been frozen by the user. 在列表中右击某一任务可以冻结或取消冻结该任务。You can freeze and unfreeze a task by right-clicking it in the list.
IDID 系统为任务提供的编号。A system-provided number for the task. 在本机代码中,该编号是任务的地址。In native code, this is the address of the task.
状态Status 任务的当前状态(已计划、活动、已阻止、已死锁、正在等待或已完成)。The current state (scheduled, active, blocked, deadlocked, awaiting, or completed) of the task. 已计划的任务是指尚未运行的任务,因此它没有调用堆栈、已分配的线程或相关信息。A scheduled task is one that has not yet been run and, therefore, does not yet have a call stack, assigned thread, or related information.

活动任务是指在中断调试器之前正在执行代码的任务。A active task is one that was executing code before breaking in the debugger.

正在等待或已阻止的任务是指因以下原因而被阻止的任务:正在等待向事件发送信号、释放锁或完成其他任务。An awaiting or blocked task is one that is blocked because it is waiting on an event to be signaled, a lock to be released, or another task to finish.

死锁任务是指其线程与其他线程相互死锁的正在等待的任务。A deadlocked task is a waiting task whose thread is deadlocked with another thread.

将鼠标悬停在已死锁或正在等待的任务的“状态”单元格上,详细了解阻止原因。Hover over the Status cell for a deadlocked or awaiting task to see more information about the block. 警告: “任务”窗口只针对使用 Wait Chain Traversal (WCT) 所支持的同步基元的受阻任务报告死锁。Warning: The Tasks window reports deadlock only for a blocked task that uses a synchronization primitive that is supported by Wait Chain Traversal (WCT). 例如,对于某个使用 WCT 的 Task 死锁对象,调试器会报告“正在等待-已死锁”。For example, for a deadlocked Task object, which uses WCT, the debugger reports Awaiting-deadlocked. 对于由并发运行时管理、不使用 WCT 的死锁任务,调试器则会报告“正在等待”。For a deadlocked task that is managed by the Concurrency Runtime, which does not use WCT, the debugger reports Waiting. 有关 WCT 的详细信息,请参阅 Wait Chain TraversalFor more information about WCT, see Wait Chain Traversal.
开始时间Start Time 任务变为活动状态的时间。The time at which the task became active.
持续时间Duration 任务已处于活动状态的秒数。The number of seconds that the task has been active.
完成时间Completion Time 任务完成的时间。The time at which the task completed.
位置Location 任务调用堆栈中的当前位置。The current location in the call stack of the task. 将鼠标指针悬停在此单元格上可以查看任务的整个调用堆栈。Hover over this cell to see the entire call stack for the task. 已计划的任务在该列中没有相应的值。Scheduled tasks do not have a value in this column.
TaskTask 创建任务时传递到该任务的初始方法以及任何参数。The initial method and any arguments that were passed to the task when it was created.
AsyncStateAsyncState 对于托管代码,即任务状态。For managed code, the task status. 默认情况下,此列被隐藏。By default, this column is hidden. 若要显示此列,请打开其中一个列标题的上下文菜单。To display this column, open the context menu for one of the column headers. 选择“列”,再选择“AsyncState”。Choose Columns, AsyncState.
父级Parent 创建此任务的任务的 ID。The ID of the task that created this task. 如果为空白,则说明该任务没有父级。If this is blank, the task has no parent. 这仅适用于托管程序。This is only applicable for managed programs.
线程分配Thread Assignment 运行任务的线程的 ID 和名称。The ID and name of the thread on which the task is running.
AppDomainAppDomain 对于托管代码,该列表示要在其中执行任务的应用程序域。For managed code, the application domain in which the task is executing.
task_grouptask_group 对于本机代码,该列表示计划任务的 task_group 对象的地址。For native code, the address of the task_group object that scheduled the task. 对于异步代理和轻量级任务,该列设置为 0。For asynchronous agents and lightweight tasks, this column is set to 0.
ProcessProcess 运行任务的进程的 ID。The ID of the process that the task is running on.

通过右击列标题并选择所需的列可以向视图添加列。You can add columns to the view by right-clicking a column heading and then selecting the columns you want. (通过清除所选内容即可删除列。)你还可以通过向左或向右拖动列来对列重新排序。(Remove columns by clearing the selections.) You can also reorder columns by dragging them left or right. 列的快捷菜单如下图所示。The column shortcut menu is shown in the following illustration.

“任务”窗口中的“快捷视图”菜单Shortcut view menu in Tasks window

对任务进行排序Sorting Tasks

若要按列条件对任务进行排序,请单击相应的列标题。To sort tasks by column criteria, click the column header. 例如,通过单击“ID”列标题,可以按任务 ID 对任务进行排序:1、2、3、4、5 等等。For example, by clicking the ID column header, you can sort the tasks by task ID: 1,2,3,4,5 and so on. 若要反转排序顺序,请再次单击相应的列标题。To reverse the sort order, click the column header again. 当前排序列和排序顺序由该列上的箭头指示。The current sort column and sort order is indicated by an arrow on the column.

对任务进行分组Grouping Tasks

你可以根据列表视图中的任何列对任务进行分组。You can group tasks based on any column in the list view. 例如,通过右击“状态”列标题并单击“按 > [状态]分组” ,可以将所有具有相同状态的任务划分为一个组。For example, by right-clicking the Status column header and then clicking Group by > [status], you can group all tasks that have the same status. 例如,你可以快速查看正在等待的任务,以便关注其被阻止的原因。For example, you could quickly see awaiting tasks so that you could focus on why they are blocked. 您还可以在调试会话期间折叠不相关的组。You can also collapse a group that is not of interest during the debug session. 同样,还可以按其他列进行分组。In the same manner, you can group by the other columns. 只需单击组标题旁的按钮即可标记或取消标记组。A group can be (un)flagged just by clicking the button next to the group header. 下图显示了处于分组模式下的“任务”窗口。The following illustration shows the Tasks window in grouped mode.

“任务”窗口中的分组模式Grouped mode in Tasks window

父子视图Parent Child View

(此视图仅可用于托管代码。)通过右键单击“状态”列标题,然后单击“分组依据” > “父视图”,你可以将任务列表更改为分层视图;在分层视图中,每个子任务都是一个子节点,可以在其父级下显示或隐藏该子节点 。(This view is available for managed code only.) By right-clicking the Status column header and then clicking Group by > Parent, you can change the list of tasks to a hierarchical view, in which every child task is a sub-node that can be displayed or hidden under its parent.

标记任务Flagging Tasks

可以标记运行任务的线程,方法是选择任务列表项,然后从上下文菜单中选择“标记分配的线程”,或者单击第一列中的旗形图标。You can flag the thread the task on which a task is running by selecting the task list item and then choosing Flag Assigned Thread from the context menu, or by clicking the flag icon in the first column. 如果标记了多个任务,则可以按标记列进行排序以在顶部显示标记的所有任务,从而仅关注这些任务。If you flag several tasks, you can then sort on the flag column to bring all the flagged tasks to the top so that you can focus just on them. 此外,还可以使用“并行堆栈”窗口仅查看已标记任务。You can also use the Parallel Stacks window to view only flagged tasks. 这样,你便可以为调试操作滤出不相关的任务。This lets you filter out tasks that you are not interested in for debugging. 标记不会在调试会话之间保留。Flags are not persisted between debugging sessions.

冻结和解冻任务Freezing and Thawing Tasks

通过右击任务列表项并单击“冻结指定的线程”,可以冻结运行任务的线程。You can freeze the thread on which a task is running by right-clicking the task list item and then clicking Freeze Assigned Thread. (如果任务已冻结,该命令则为“解冻指定的线程”。)如果已冻结线程,则逐句通过当前断点后的代码时,将不会执行该线程。(If a task is already frozen, the command is Thaw Assigned Thread.) When you freeze a thread, that thread will not execute when you step through code after the current breakpoint. “冻结此线程之外的所有线程”命令冻结除正在执行任务列表项的线程以外的所有线程。The Freeze All Threads But This One command freezes all threads except the one that is executing the task list item.

下图所示为每项任务的其他菜单项。The following illustration shows the other menu items for each task.

“任务”窗口中的“快捷线程”菜单Shortcut thread menu in Tasks window

切换活动任务或帧Switching the Active Task or Frame

“切换到任务”命令使当前任务成为活动任务。The Switch to Task command makes the current task the active task. “切换到帧”命令使所选堆栈帧成为活动堆栈帧。The Switch to Frame command makes the selected stack frame the active stack frame. 调试器上下文切换到当前任务或所选堆栈帧。The debugger context switches to the current task or the selected stack frame.

请参阅See also