1

Why do System.Diagnostics.GetCurrentProcess().Threads.Count, the Task Manager, and the Visual Studio Debugger all report a different number of threads?

I found this: Why does the number of threads reported by WinDbg, Task Manager and VS Debugger differ?

And it indicated that the debugger performs some form of filtering, but there was never an answer as to what the filter was. There was also a note about invasive vs non-invasive methods of obtaining the thread count and how that might effect the reported amount by the task manager.

What I am seeing is that System.Diagnostics.GetCurrentProcess().Threads.Count reports a larger number of threads than the Task Manager, which reports a larger number of threads than the Debugger.

Is System.Diagnostics.GetCurrentProcess().Threads.Count ultimately the most accurate or is there something erroneous or misleading in what it reports? It would also be nice to know what filtering is being applied by the Visual Studio Debugger (and seemingly the Task Manager as well, albeit a different filter).

Community
  • 1
  • 1
  • 5
    Note that depending on the actual execution the number of threads could change rather quickly so if two thread counters are not sampling at the same rate they might show different numbers. – Emond Aug 25 '14 at 18:58
  • I set a breakpoint for the Visual Studio debugger immediately after retrieving the diagnostics count. I'm not certain, but I'd hope the debugger and the diagnostics would have checked the thread count at roughly the same time under that circumstance. – Christopher Griffith Aug 25 '14 at 19:57
  • Unless you find a way to compare both sets of threads, we're just guessing. – Emond Aug 25 '14 at 20:04

1 Answers1

1
  1. One thing that could cause differences between these counts is the sampling rate of the counters.

    Depending on the actual execution, the number of threads could change rather quickly so if two thread counters are not sampling at the same rate they might show different numbers.

  2. I am not aware of any filtering in visual Studio but this answer: https://stackoverflow.com/a/2295054/563088 has an interesting find: dead threads. Note that during debugging Visual Studio pauses the process so threads (especially at OS level) could be gone but still be present at a managed level or vise versa.

Community
  • 1
  • 1
Emond
  • 50,210
  • 11
  • 84
  • 115