问题描述
例如在 C# 中调试线程时,您可以看到每个线程的 ID.
In C# when debugging threads for example, you can see each thread's ID.
我找不到以编程方式获得相同线程的方法.我什至无法获得当前线程的 ID(在 Thread.currentThread
的属性中).
I couldn't find a way to get that same thread, programmatically. I could not even get the ID of the current thread (in the properties of the Thread.currentThread
).
那么,我想知道Visual Studio如何获取线程的ID,有没有办法获取id为2345
的线程的句柄,例如?
So, I wonder how does Visual Studio get the IDs of the threads, and is there a way to get the handle of the thread with id 2345
, for example?
推荐答案
GetThreadId
返回给定本机线程的 ID.有很多方法可以使它与托管线程一起工作,我敢肯定,您只需要找到线程句柄并将其传递给该函数即可.
GetThreadId
returns the ID of a given native thread. There are ways to make it work with managed threads, I'm sure, all you need to find is the thread handle and pass it to that function.
GetCurrentThreadId
返回当前线程的 ID.
GetCurrentThreadId
returns the ID of the current thread.
GetCurrentThreadId
自 .NET 2.0 起已被弃用:推荐的方法是 Thread.CurrentThread.ManagedThreadId
属性.
GetCurrentThreadId
has been deprecated as of .NET 2.0: the recommended way is the Thread.CurrentThread.ManagedThreadId
property.
这篇关于从线程中获取线程 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!