views:

1299

answers:

5

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?

+3  A: 

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
A: 

Maybe this snippet can help?

Hosam Aly
That enumerates the `ProcessThread`s - i.e. not rally the managed `Thread` class, which is (I expect) what is intended.
Marc Gravell
It's true - I knew about enumerating native threads, but I want more specific managed thread information (like the name).
Jon Tackabury
Thanks for the information. I did not know that.
Hosam Aly
A: 

If you want the threads for a particular process, you can use

Process.GetProcessesByName(processName);

This returns an array of Process objects, which have a Threads property.

Scott Dorman
That enumerates the `ProcessThread`s - i.e. not rally the managed `Thread` class, which is (I expect) what is intended.
Marc Gravell
+3  A: 

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.

Marc Gravell
This is what I may end up doing, I was just hoping there was already something doing it for me. :)
Jon Tackabury
A: 

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