As an exercise I was writing some code to display the O/S processes and O/S threads within a process (like Sysinternals process explorer does).
I found that .net's ManagedThreadId(s) are not the O/S thread ids. After a bit of reading I came across AppDomain.GetCurrentThreadId(). Unfortunately, that function is marked as "obsolete" (which could mean "not available" in the future). One solution I found is to use InteropServices to directly call the Win32 GetCurrentThreadId. I am fine with that but, it feels counter to the .net philosophy.
My question is: is there a CLR "friendly" way of obtaining the real id of the current thread ?
For reference, here is a snippet of code showing what I've tried so far. // 1 and // 2 display the correct thread id, // 3 and // 4 were attempts to obtain the same info in a CLR friendly way (but they don't work.)
Thank you for your help,
John.
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
static void Main(string[] args)
{
// AppDomain.GetCurrentThreadId() is "obsolete"
int ThreadId1 = AppDomain.GetCurrentThreadId(); // 1
// not the ".net" way of doing things
int ThreadId2 = GetCurrentThreadId(); // 2
// "no joy" attempts to get the same results I got above
int ThreadId3 = Process.GetCurrentProcess().Threads[0].Id; // 3
int ThreadId4 = Thread.CurrentThread.ManagedThreadId; // 4
Console.WriteLine("ThreadId1: {0}, ThreadId2: {1}, ThreadId3: {2}, " +
"ThreadId4: {3}",
ThreadId1, ThreadId2, ThreadId3, ThreadId4);
}