views:

420

answers:

3

I'm trying to monitor the states of a group of threads in a Microsoft.NET application written in C#. I'd like to be able to also monitor any child threads spawned by the original threads.

In Java, you can assign threads to a thread group, and their children will also belong to the group. Is there an equivalent in .NET?

I briefly looked at ExecutionContext and LogicalCallContext, but I can't see how to find all the threads that are in a context. Raymond Chen has an article about a Win32 API method for enumerating threads, but I hope I don't have to go that low.

+2  A: 

You can enumerate the threads in your process using the Threads property of System.Diagnostics.Process.

Note however, that the objects you get here are not of the same type as those you create to start threads yourself (i.e. are not System.Threading.Thread objects).

A concept of thread groups does not exist however, AFAIK.

Christian.K
+2  A: 

They are working on something like that in their "Task" API, which is part of Parallel Extensions.

Jonathan Allen
+1  A: 

Make it simple:

Create your ThreadGroup class with a method wrapping the thread creation process.

When this method is called, it adds the created thread to a Collection and there is your group.

andrecarlucci
That's a good idea if you control all the code that's launching threads, but I don't.
Don Kirkby