25

Is it possible to enumerate all managed threads in C#? Visual Studio seems to be able to do this when you hit a break point while debugging. In the "Threads" window it shows a list of all running threads, including managed names. Does anyone know how it does this?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
  • @Marc: I thought there must be a different way, as VS can enumerate them. I might end up tracking them myself though. – Jon Tackabury Jan 21 '09 at 20:26
  • 1
    @Marc, I disagree. Maybe he's working on an IDE and wants to include debugging capabilities. "Keep track of your own threads" is not a valid answer in this case. The question could be marked as duplicate, but the "short answer" is not the best one. – Hosam Aly Jan 21 '09 at 20:28
  • @Hosam Aly: OK, I'll accept that... I'll rollback and re-post as a reply – Marc Gravell Jan 21 '09 at 20:31
  • 1
    See an answer here: http://stackoverflow.com/questions/10315862/get-list-of-threads/35558240#35558240 – Contango Feb 22 '16 at 16:08

4 Answers4

6

Debuggers are often able to do things "normal" code can't. You'd probably find that you can do this if you use the profiling/debugging API, but I don't believe you can do it from "normal" .NET code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I don't see how to do it from a debugger either. It appears that the thread ids given in the debugger are the Win32 ids rather than the managed thread ids. – CrazyCasta Oct 04 '12 at 00:56
  • 1
    @CrazyCasta: I'm not saying you can explicitly do it *from* a debugger. I'm saying that when the debugger *does* list all threads, it can use APIs which aren't available elsewhere. Even if it lists Win32 *ids*, it still shows managed thread *names*, doesn't it? – Jon Skeet Oct 04 '12 at 05:48
  • I see what appear to be managed names, however there's a few problems. 1. It could be that the managed code is just setting the name of the unmanaged thread. 2. As has been stated, there is no defined 1:1 correlation between unmanaged and managed threads. Each unmanaged thread could be switching between two managed threads. 3. I would guess that the debugger is just enumerating unmanaged threads. – CrazyCasta Oct 04 '12 at 19:15
6

This sounds like a duplicate of "How to enumerate threads in .NET using the Name property?" - If so, the short answer is "keep track of your own threads yourself" - i.e. in a List<Thread> or similar.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Take a look at Managed Stack Explorer:

MSE works by quickly attaching to a process when a stack trace is requested and the detaching the second the stack trace has been retrieved. This way the interference in the normal operation of the process is minimized.

Unfortunately, this means it has to done by an external process.

A similar tool is StackDump, which uses MDbg to generate the dump.

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
0

You can enumerate threads by making an HTTP GET request to ProcInsp. ProcInsp allows you to get threads with stack traces of CLR processes (either in its UI or as a JSON message). The web API is available at /Process/%PID%/Threads.

Disclaimer: I am the developer of this tool. It is MIT-licensed and free for use.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574