Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:32+00:00 2026-05-21T06:38:32+00:00

As an exercise I was writing some code to display the O/S processes and

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:38:33+00:00Added an answer on May 21, 2026 at 6:38 am

    PInvoking into the GetCurrentThreadId is your best bet and will give you the correct information.

    However I must warn you, there are very good reasons why the CLR doesn’t provide this information: it’s almost a completely useless value for managed code. It’s perfectly legal from a CLR perspective for a single managed thread to be backed by several different native threads during it’s lifetime. This means the result of GetCurrentThreadId can (and will) change throughout the course of a thread’s lifetime.

    In many applications this is not an observable phenomenon. In a UI application this won’t actually happen because it’s typically backed by an STA thread which is harder (usually even illegal) to swap out due to COM interop issues. So many developers are blissfully ignorant of this. However it’s very easy to swap out MTA threads under the hood which is typically the execution context of a background thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing some C# code for a training exercise, in which I
I'm writing some new code that involves POP3 manipulation. I'm trying to come up
I'm writing an exercise in C++, and I wrote a code that seems to
I'm writing a small interpreter for a simple BASIC like language as an exercise
I have some objective-c code that uses an NSLock to implement a sort of
I ran into a snag recently. I am writing some unit tests to exercise
I'm writing a math library as a practical exercise. I've run into some problems
I'm currently in the process of writing a client server app as an exercise
I'm trying to solve an exercise which requires that : the starting process must
I am writing a huffman implementation in Python as a learning exercise. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.