本文翻译自:Getting the thread ID from a thread
In C# when debugging threads for example, you can see each thread's ID. 例如,在调试线程的C#中,您可以看到每个线程的ID。
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
). 我甚至无法获取当前线程的ID(在Thread.currentThread
的属性中)。
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? 所以,我想知道Visual Studio如何获取线程的ID,有没有办法获得id为2345
的线程句柄?
#1楼
参考:https://stackoom.com/question/72qZ/从线程获取线程ID
#2楼
要获取操作系统ID,请使用:
AppDomain.GetCurrentThreadId()
#3楼
System.Threading.Thread.CurrentThread.Name
System.Threading.Thread.CurrentThread.ManagedThreadId
#4楼
GetThreadId
returns the ID of a given native thread. GetThreadId
返回给GetThreadId
机线程的ID。 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
returns the ID of the current thread. GetCurrentThreadId
返回当前线程的ID。
GetCurrentThreadId
has been deprecated as of .NET 2.0: the recommended way is the Thread.CurrentThread.ManagedThreadId
property. 从.NET 2.0开始,不推荐使用GetCurrentThreadId
:建议的方法是Thread.CurrentThread.ManagedThreadId
属性。
#5楼
To find the current thread Id use - `Thread.CurrentThread.ManagedThreadId'. 要查找当前线程Id,请使用 - “Thread.CurrentThread.ManagedThreadId”。 But in this case you might need the current win32 thread id - use pInvoke to get it with this function: 但在这种情况下,您可能需要当前的win32线程ID - 使用pInvoke来获取此函数:
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();
First you'll need to save the managed thread id and win32 thread id connection - use a dictionary that maps a win32 id to managed thread. 首先,您需要保存托管线程ID和win32线程ID连接 - 使用将win32 id映射到托管线程的字典。
Then to find a thread by it's id iterate over the process's thread using Process.GetCurrentProcess().Threads and find the thread with that id: 然后通过它的id找到一个线程使用Process.GetCurrentProcess()迭代进程的线程。线程并找到具有该id的线程:
foreach (ProcessThread thread in Process.GetCurrentProcess().Threads)
{
var managedThread = win32ToManagedThread[thread.id];
if((managedThread.ManagedThreadId == threadId)
{
return managedThread;
}
}
#6楼
From managed code you have access to instances of the Thread
type for each managed thread. 从托管代码中,您可以访问每个托管线程的Thread
类型实例。 Thread
encapsulates the concept of an OS thread and as of the current CLR there's a one-to-one correspondance with managed threads and OS threads. Thread
封装了OS线程的概念,并且从当前的CLR开始,与托管线程和OS线程一一对应。 However, this is an implementation detail, that may change in the future. 但是,这是一个实现细节,可能在将来发生变化。
The ID displayed by Visual Studio is actually the OS thread ID. Visual Studio显示的ID实际上是OS线程ID。 This is not the same as the managed thread ID as suggested by several replies. 这是不一样的托管线程ID几种答复的建议。
The Thread
type does include a private IntPtr member field called DONT_USE_InternalThread
, which points to the underlying OS structure. Thread
类型确实包含一个名为DONT_USE_InternalThread
的私有IntPtr成员字段,该字段指向底层OS结构。 However, as this is really an implementation detail it is not advisable to pursue this IMO. 但是,由于这实际上是一个实施细节,因此不宜采用这种IMO。 And the name sort of indicates that you shouldn't rely on this. 名称有点表明你不应该依赖于此。