2

Does anyone know if it is possible to detect when a new thread has been created in a managed environment?

I would like to have a list of all threads in my application (id and name of each thread). I am using Concurrency Visualizer in Visual Studio and it only displays thread ids (not thread names). And it's quite difficult to visualize the program flow if you have 20+ threads without their names.

EDIT: Using Process.GetCurrentProcess(). Threads is not OK as the CLR does not guarantee one-to-one mapping between managed and unmanaged threads.

One solution would be to create some sort of thread manager via which all thread creation would have to be done. It would also manage list of all currently alive threads.

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
Zvonko
  • 363
  • 2
  • 19
  • 3
    why? (What is the real problem you are trying to solve) – Ian Ringrose Jul 20 '11 at 12:15
  • Your `Process` object will have a list of all threads. Is that all you need? If you actually want to be notified when a new thread is created, you probably have to have use a debugging/profiling interface. – Gabe Jul 20 '11 at 12:17
  • 1
    That requires a debugger. Programs cannot debug themselves. Whatever problem you're trying to solve, this is not the right way to solve it. – Hans Passant Jul 20 '11 at 12:18
  • I have explained my problem in more details above. – Zvonko Jul 20 '11 at 12:48

2 Answers2

4

The answer is no you can't - you can't even enumerate all current "managed" threads, let alone get notified when one is created / destroyed.

See How can I enumerate all managed threads in C#?

Note: You can enumerate all thread in a process using the Process class:

Process.GetCurrentProcess().Threads

This doesn't tell you which of those is "managed" however, and doesn't notify you when a thread is created or destroyed.

You possibly could try and figure out which of those threads are "managed" by looking throug the stack trace for managed threads, but I have honestly no idea if this would actually work for unmanaged threads and this is all starting to look really dodgey!

new StackTrace().GetFrame(someThread)

Just to clarify - you almost certainly shouldn't be doing any of this, its just some background.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
  • Using Process.GetCurrentProcess().Threads is not OK as the CLR does not guarantee one-to-one mapping between managed and unmanaged threads. – Zvonko Jul 20 '11 at 13:17
  • Zvonko What do you mean by one-to-one mapping? If you mean that the `Threads` collection may contain unmanaged threads then it almost certainly will. – Justin Jul 20 '11 at 13:22
  • An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the Fiber API to schedule many managed threads against the same operating system thread, or to move a managed thread among different operating system threads. See http://msdn.microsoft.com/en-us/library/74169f59.aspx. – Zvonko Jul 20 '11 at 13:28
1

A solution could be to store your threads in a Queue when you create them and check regularly the content of the Queue. Not a perfect solution though.

Otiel
  • 18,404
  • 16
  • 78
  • 126
  • 1
    This solution implies that you control every thread which is hardly the case (eg: ThreadPool). – Julien Lebosquain Jul 20 '11 at 12:47
  • @Julien Well, it depends on what threads you want to monitor. It works if your only need is to manage threads you have explicitly created. – Otiel Jul 20 '11 at 13:01
  • List<> is a better option, IMHO. However, in this solution you have to create your own "thread manager", abandon direct thread creation and ThreadPool use, create wrapper in thread manager for both and use strictly those wrappers. – Zvonko Jul 20 '11 at 13:17