How to get an instance of a thread from its name/id?
Is it possible to get开发者_运维技巧 the instance of a specific running thread knowing its name or id? If yes, how?
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.
You could try something like this:
Thread thread = Process.GetCurrentProcess().Threads.Single(t => t.ManagedThreadId == threadId);
You forgot to use Cast.
Process.GetCurrentProcess().Threads.Cast<ProcessThread>().Single(t => t.ManagedThreadId == threadId);
精彩评论