开发者

How to get an instance of a thread from its name/id?

开发者 https://www.devze.com 2023-01-06 06:10 出处:网络
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 lo

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消