13

Possible Duplicate:
C#/.NET: How to get the thread id from a thread?

How I can get the same thread ID as I see it in Visual Studio?

I have tried to use Thread.CurrentThread.ManagedThreadId, but I am getting different numbers.

I am getting 35, 38, 39, etc., but in Visual Studio I have 10596, 893, etc...

Community
  • 1
  • 1
Night Walker
  • 20,638
  • 52
  • 151
  • 228

3 Answers3

15

Use GetCurrentThreadId() or ManagedThreadId() to get the thread ID:

int threadID = (int)AppDomain.GetCurrentThreadId();
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("ThreadId = " + threadID);
Console.WriteLine("ManagedThreadId = " + managedThreadId);

Have a look at Stack Overflow question Getting the thread ID from a thread.

Community
  • 1
  • 1
Bibhu
  • 4,053
  • 4
  • 33
  • 63
  • 3
    `System.AppDomain.GetCurrentThreadId()` is obsolete/deprecated. – testing Dec 23 '14 at 07:56
  • 2
    Deprecated but if you're trying to map to what you see in the "threads" dropdown in visual studio it's still the way to go – Ready Cent Nov 18 '16 at 16:32
  • I wonder what the the alternative to get the ThreadID is now. I can't find one anywhere. And don't say ManagedThreadId lol. The thing is... when Threads Exit... The Output in Visual Studio outputs the threads 'ThreadID', NOT the ManagedThreadId. So for me the ManagedThreadId was no good as it didn't tell me what I needed to know. I used the Deprecated method and converted the Int to Hex as Visual Studio Outputs the ThreadId in Hex. `Hex(AppDomain.GetCurrentThreadId())` – Code Novice Feb 21 '19 at 23:15
3

You can use WinApi functions GetCurrentThreadId and GetThreadId

Stecya
  • 22,896
  • 10
  • 72
  • 102
2

If you are seeing a different thread ID in your live application as opposed to when you debug in Visual Studio, that is just what you should expect to see, right?

When running in the debugger, you are effectively running the application in the debugger host which will have different threads than just running the application on its own.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Colin Desmond
  • 4,824
  • 4
  • 46
  • 67