Getting the thread ID from a thread
In C# when debugging threads for example, you can see each thread's ID.
I couldn't开发者_StackOverflow中文版 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
).
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
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
returns the ID of the current thread.
GetCurrentThreadId
has been deprecated as of .NET 2.0: the recommended way is the Thread.CurrentThread.ManagedThreadId
property.
In C# when debugging threads for example, you can see each thread's ID.
This will be the Ids of the managed threads. ManagedThreadId
is a member of Thread
so you can get the Id from any Thread object. This will get you the current ManagedThreadID:
Thread.CurrentThread.ManagedThreadId
To get an OS thread by its OS thread ID (not ManagedThreadID), you can try a bit of linq.
int unmanagedId = 2345;
ProcessThread myThread = (from ProcessThread entry in Process.GetCurrentProcess().Threads
where entry.Id == unmanagedId
select entry).First();
It seems there is no way to enumerate the managed threads and no relation between ProcessThread and Thread, so getting a managed thread by its Id is a tough one.
For more details on Managed vs Unmanaged threading, see this MSDN article.
You can use the deprecated AppDomain.GetCurrentThreadId
to get the ID of the currently running thread. This method uses a PInvoke to the Win32 API method GetCurrentThreadID
, and will return the Windows thread ID.
This method is marked as deprecated because the .NET Thread object does not correspond to a single Windows thread, and as such there is no stable ID which can be returned by Windows for a given .NET thread.
See configurator's answer for more reasons why this is the case.
To get the OS ID use:
AppDomain.GetCurrentThreadId()
According to MSDN:
An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the CLR Hosting API to schedule many managed threads against the same operating system thread, or to move a managed thread between different operating system threads.
So basically, the Thread
object does not necessarily correspond to an OS thread - which is why it doesn't have the native ID exposed.
For those about to hack:
public static int GetNativeThreadId(Thread thread)
{
var f = typeof(Thread).GetField("DONT_USE_InternalThread",
BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
var pInternalThread = (IntPtr)f.GetValue(thread);
var nativeId = Marshal.ReadInt32(pInternalThread, (IntPtr.Size == 8) ? 548 : 348); // found by analyzing the memory
return nativeId;
}
To find the current thread Id use - `Thread.CurrentThread.ManagedThreadId'. But in this case you might need the current win32 thread id - use pInvoke to get it with this function:
[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.
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:
foreach (ProcessThread thread in Process.GetCurrentProcess().Threads)
{
var managedThread = win32ToManagedThread[thread.id];
if((managedThread.ManagedThreadId == threadId)
{
return managedThread;
}
}
The offset under Windows 10 is 0x022C (x64-bit-Application) and 0x0160 (x32-bit-Application):
public static int GetNativeThreadId(Thread thread)
{
var f = typeof(Thread).GetField("DONT_USE_InternalThread",
BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
var pInternalThread = (IntPtr)f.GetValue(thread);
var nativeId = Marshal.ReadInt32(pInternalThread, (IntPtr.Size == 8) ? 0x022C : 0x0160); // found by analyzing the memory
return nativeId;
}
System.Threading.Thread.CurrentThread.Name
System.Threading.Thread.CurrentThread.ManagedThreadId
From managed code you have access to instances of the Thread
type for each managed 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. However, this is an implementation detail, that may change in the future.
The ID displayed by Visual Studio is actually the OS thread ID. This is not the same as the managed thread ID as suggested by several replies.
The Thread
type does include a private IntPtr member field called DONT_USE_InternalThread
, which points to the underlying OS structure. However, as this is really an implementation detail it is not advisable to pursue this IMO. And the name sort of indicates that you shouldn't rely on this.
You can use Thread.GetHashCode, which returns the managed thread ID. If you think about the purpose of GetHashCode, this makes good sense -- it needs to be a unique identifier (e.g. key in a dictionary) for the object (the thread).
The reference source for the Thread class is instructive here. (Granted, a particular .NET implementation may not be based on this source code, but for debugging purposes I'll take my chances.)
GetHashCode "provides this hash code for algorithms that need quick checks of object equality," so it is well-suited for checking Thread equality -- for example to assert that a particular method is executing on the thread you wanted it called from.
As of July 2022 the VS2022 IDE suggests using System.Environment.CurrentManagedThreadId
instead of Thread.CurrentThread.ManagedThreadId
Quoting from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1840:
System.Environment.CurrentManagedThreadId is a compact and efficient replacement of the Thread.CurrentThread.ManagedThreadId pattern.
精彩评论