我们有一个应用程序可以启动数组的任务(System.Threading.Tasks.Task)。最后,它使用以下代码等待任务结束:
Dim tasks As New List(Of Task)
For Each fator As String In Fatores
tasks.Add( _
Task.Run(Sub()
Calcular(New Object() {fator,})
End Sub))
Next
Task.WaitAll(tasks.ToArray())
在执行期间,我提取了一个完整的应用程序转储。使用!Threads我得到了下面的列表(最终删除了)。
ID
OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception
0 1 60d8 000000000045e540 26020 Preemptive 000000014DE5FE38:000000014DE5FFD0 00000000004492e0 0 STA
2 2 3c90 00000000004896d0 2b220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA (Finalizer)
5 3 309c 000000001d6ba3f0 102a220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA (Threadpool Worker)
7 4 743c 000000001d6eb920 202b220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA
8 5 7528 000000001d794080 202b220 Preemptive 000000014DE596F8:000000014DE59FD0 00000000004492e0 0 MTA
9 6 53f4 000000001d7bd720 21220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 Ukn
10 17 239c 000000001fa04160 202b020 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA
12 1134 20ec 0000000000474380 8029220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA (Threadpool Completion Port)
15 649 2e2c 00000000247fdd00 1029220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA (Threadpool Worker)
17 650 28ac 0000000000478bb0 1029220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA (Threadpool Worker)
16 651 47c8 000000006069d5a0 1029220 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 MTA (Threadpool Worker)
XXXX 706 0 000000001fb2f9f0 1039820 Preemptive 0000000000000000:0000000000000000 00000000004492e0 0 Ukn (Threadpool Worker)
22 705 5dd0 000000001fb301c0 1029220 Preemptive 000000014DE5C098:000000014DE5DFD0 00000000004492e0 0 MTA (Threadpool Worker)
使用命令〜* e!clrstack我可以在此行找到任务列表
000000002083ead0 000007fee6c3bf95 System.Threading.Tasks.Task.WaitAllBlockingCore(System.Collections.Generic.List`1, Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 5193]
000000002083eb70 000007fee6c3bbd0 System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 5109]
我在下面的参数中获得了任务列表
0:010> !dumparray 0000000120ca19d8 000000002083eb70 000007fee6c3bbd0 System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 5109]
PARAMETERS:
tasks (0x000000002083ec90) = 0x0000000120ca19d8
millisecondsTimeout (0x000000002083ec98) = 0x00000000ffffffff
cancellationToken = <no data>
Name: System.Threading.Tasks.Task[]
MethodTable: 000007fee6f424c8
EEClass: 000007fee5e31238
Size: 72(0x48) bytes
Array: Rank 1, Number of elements 6, Type CLASS
Element Methodtable: 000007fee642c650
[0] 0000000120ca1048
[1] 0000000120ca11f0
[2] 0000000120ca1398
[3] 0000000120ca1540
[4] 0000000120ca16e8
[5] 0000000120ca1890
每个任务都有下面的结构,其中显示只有活动任务具有填充字段m_taskID
0:010> !DumpObj /d 0000000120ca1048
Name: System.Threading.Tasks.Task
MethodTable: 000007fee642c650
EEClass: 000007fee5e36a30
Size: 72(0x48) bytes
File: C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Fields:
MT Field Offset Type VT Attr Value Name
000007fee6419540 4001a16 38 System.Int32 1 instance 42082 m_taskId
000007fee64170f0 4001a17 8 System.Object 0 instance 0000000000000000 m_action
000007fee64170f0 4001a18 10 System.Object 0 instance 0000000000000000 m_stateObject
000007fee64202b8 4001a19 18 ...sks.TaskScheduler 0 instance 0000000003e83d28 m_taskScheduler
000007fee642c650 4001a1a 20 ...eading.Tasks.Task 0 instance 0000000000000000 m_parent
000007fee6419540 4001a1b 3c System.Int32 1 instance 16982024 m_stateFlags
000007fee64170f0 4001a2e 28 System.Object 0 instance 0000000003e83da8 m_continuationObject
000007fee64247e8 4001a33 30 ...tingentProperties 0 instance 0000000120ca1148 m_contingentProperties
000007fee6419540 4001a14 f30 System.Int32 1 shared static s_taskIdCounter
>> Domain:Value 00000000004492e0:42084 <<
000007fee6430ac8 4001a15 e80 ...Tasks.TaskFactory 0 shared static s_factory
>> Domain:Value 00000000004492e0:0000000003e83d80 <<
000007fee64170f0 4001a2f e88 System.Object 0 shared static s_taskCompletionSentinel
>> Domain:Value 00000000004492e0:0000000003e83da8 <<
000007fee6439b50 4001a30 f34 System.Boolean 1 shared static s_asyncDebuggingEnabled
>> Domain:Value 00000000004492e0:0 <<
000007fee5d942c8 4001a31 e90 ....Task, mscorlib]] 0 shared static s_currentActiveTasks
>> Domain:Value 00000000004492e0:0000000003e83dc0 <<
000007fee64170f0 4001a32 e98 System.Object 0 shared static s_activeTasksLock
>> Domain:Value 00000000004492e0:0000000003e83e10 <<
000007fee6492148 4001a34 ea0 ...bject, mscorlib]] 0 shared static s_taskCancelCallback
>> Domain:Value 00000000004492e0:0000000003e83e28 <<
000007fee6492288 4001a35 ea8 ...rties, mscorlib]] 0 shared static s_createContingentProperties
>> Domain:Value 00000000004492e0:0000000003e83e68 <<
000007fee642c650 4001a36 eb0 ...eading.Tasks.Task 0 shared static s_completedTask
>> Domain:Value 00000000004492e0:0000000000000000 <<
000007fee6492320 4001a37 eb8 ....Task, mscorlib]] 0 shared static s_IsExceptionObservedByParentPredicate
>> Domain:Value 00000000004492e0:0000000003e83ec0 <<
000007fee6427a38 4001a38 ec0 ...g.ContextCallback 0 shared static s_ecCallback
>> Domain:Value 00000000004492e0:0000000003e84910 <<
000007fee64923b0 4001a39 ec8 ...bject, mscorlib]] 0 shared static s_IsTaskContinuationNullPredicate
>> Domain:Value 00000000004492e0:0000000003e83f00 <<
000007fee642c650 4001a12 28 ...eading.Tasks.Task 0 shared TLstatic t_currentTask
>> Thread:Value 60d8:0000000000000000 239c:0000000000000000 2e2c:0000000120ca1540 28ac:0000000120ca16e8 47c8:0000000120ca1890 <<
000007fee649d940 4001a13 30 ....Tasks.StackGuard 0 shared TLstatic t_stackGuard
>> Thread:Value 60d8:0000000005d88880 239c:0000000003fbbdd8 2e2c:0000000000000000 28ac:0000000000000000 47c8:0000000000000000 <<
我想知道如何将此任务与上面列表中的右线程相关联。最后的字段t_currentTask和t_stackGuard具有所有有效的线程 ,包括启动任务的STA(Windows窗体)。我看了一下数组的另一个任务,最后两个字段在所有这些中都是相同的,没有关于相关线程的线索。我可以识别(通过线程的!clrstack中的方法名称)哪些线程正在执行任务,但我无法关联任务X线程。 谁能帮助我做到这一点?提前谢谢!
答案 0 :(得分:0)
我找到了一些帮助过我的微软人。如果运行任务的线程是&#34; alive&#34;,我只能找到它。只需跟踪堆中的实例,如下所示。但是,与我的假设相反,m_taskId = 0的Task对象是正在运行的Tasks。因此,如果有任何进程被锁定等待某个任务,如果我没有在任何地方找到它,因为它已中止并且没有向等待其结束的人报告状态。
0:010> !DumpArray /d 0000000120ca19d8
Name: System.Threading.Tasks.Task[]
MethodTable: 000007fee6f424c8
EEClass: 000007fee5e31238
Size: 72(0x48) bytes
Array: Rank 1, Number of elements 6, Type CLASS
Element Methodtable: 000007fee642c650
[0] 0000000120ca1048
[1] 0000000120ca11f0
[2] 0000000120ca1398
[3] 0000000120ca1540
[4] 0000000120ca16e8
[5] 0000000120ca1890
0:010> !gcroot 0000000120ca1890
Thread 239c:
000000002083ead0 000007fee6c3bf95 System.Threading.Tasks.Task.WaitAllBlockingCore(System.Collections.Generic.List`1<System.Threading.Tasks.Task>, Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 5193]
rbp+10: 000000002083eb70
-> 0000000120ca1a20 System.Collections.Generic.List`1[[System.Threading.Tasks.Task, mscorlib]]
-> 0000000120ca1a48 System.Threading.Tasks.Task[]
-> 0000000120ca1890 System.Threading.Tasks.Task
000000002083eb70 000007fee6c3bbd0 System.Threading.Tasks.Task.WaitAll(System.Threading.Tasks.Task[], Int32, System.Threading.CancellationToken) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 5109]
rbp+10: 000000002083ec90
-> 0000000120ca19d8 System.Threading.Tasks.Task[]
-> 0000000120ca1890 System.Threading.Tasks.Task
Thread 47c8:
000000002059e020 000007fee629c686 System.Threading.Tasks.Task.Execute() [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 2498]
rbp+10: 000000002059e060
-> 0000000120ca1890 System.Threading.Tasks.Task
000000002059e060 000007fee6274750 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs @ 954]
r14:
-> 0000000120ca1890 System.Threading.Tasks.Task
000000002059e160 000007fee629c928 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 2827]
rbp+18: 000000002059e218 (interior)
-> 0000000120cb0fe8 System.Object[]
-> 0000000120ca1890 System.Threading.Tasks.Task
000000002059e160 000007fee629c928 System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef) [f:\dd\ndp\clr\src\BCL\system\threading\Tasks\Task.cs @ 2827]
rbp+10: 000000002059e210
-> 0000000120ca1890 System.Threading.Tasks.Task
000000002059e250 000007fee627f74e System.Threading.ThreadPoolWorkQueue.Dispatch() [f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs @ 820]
rbp-20: 000000002059e2c0
-> 0000000120ca1890 System.Threading.Tasks.Task
Found 7 unique roots (run '!GCRoot -all' to see all roots).
那是