views:

55

answers:

2

Hi,

Is it possible to get the instance of a specific running thread knowing its name or id? If yes, how?

Thank you

+1  A: 

Not sure if there's a more direct way but in the worst case you should be able to loop through all the threads in Process.Threads and checking the ProcessThread.Id of them.

Here's the MSDN doc for ProcessThread properties.

ho1
Thanks. I'm gonna try it.
Amokrane
Hmm. Seems like I can only do my comparison based on the ids, not the names of the threads (Thread.Name).
Amokrane
@Amokrane: Yes, I get the feeling that that's by design. I've never set a thread's name and looking around it seems like that feature is mostly for debugging purposes. I haven't seen anything saying that outright but you can get indications here: http://social.msdn.microsoft.com/Forums/en/vsdebug/thread/2a754d46-84d0-45ec-a7bf-58e22331e1c2 and here http://msdn.microsoft.com/en-us/library/w15yf86f%28VS.71%29.aspx
ho1
+3  A: 

You could try something like this:

Thread thread = Process.GetCurrentProcess().Threads.Single(t => t.ManagedThreadId == threadId);

HTH

ema
+1 for using ManagedThreadId
Bear Monkey