Getting the thread ID [duplicate]

2019-06-15 05:22发布

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...

3条回答
别忘想泡老子
2楼-- · 2019-06-15 05:43

You can use WinApi functions GetCurrentThreadId and GetThreadId

查看更多
家丑人穷心不美
3楼-- · 2019-06-15 05:51

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.

查看更多
神经病院院长
4楼-- · 2019-06-15 06:00

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.

查看更多
登录 后发表回答