get real/unmanaged/kernel thread ID (not managed thread ID) for athread

  • Thread starter Thread starter Ashutosh
  • Start date Start date
A

Ashutosh

Hi,
How can I get the real thread ID for a thread? The Thread class provides
the managed thread ID only. Do I need to use P/Invoke for some win32
function like GetThreadID or something similar??

Regards,
Ashutosh
 
I just need the real thread ID for debugging and testing my application
for leaks. thats it :)
 
Hello Ashutosh,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you. We can get the current
Thread's OSID by calling AppDomain.GetCurrentThreadId(). This function will
return the same result as we call the Winows API GetCurrentThreadID() using
PInvoke. But that function is deprecated because it does not provide a
stable Id when managed threads are running on fibers (aka lightweight
threads).

From your description, if getting the OS thread ID is for debugging and
testing purpose, may I suggest to use the SOS debug extension and its
!threads command in Visual Studio instead? This command will print out all
managed threads' statistics information including the OSID. The following
is a typical output for the !threads command and the numbers 5060/5536/5976
are the OS thread IDs:

------------------------------------------------------------------------
!threads
PDB symbol for mscorwks.dll not loaded
ThreadCount: 3
UnstartedThread: 0
BackgroundThread: 1
PendingThread: 0
DeadThread: 0
Hosted Runtime: no
PreEmptive GC Alloc Lock
ID OSID ThreadOBJ State GC Context Domain
Count APT Exception
5060 1 13c4 0058a780 86028 Enabled 01c5a1d8:01c5bfe8 00550810 0
STA
5536 2 15a0 00558628 b228 Enabled 00000000:00000000 00550810 0
MTA (Finalizer)
5976 3 1758 005e37d8 8b028 Enabled 00000000:00000000 00550810 0
MTA
-------------------------------------------------------------------------

You can get the more detailed information about loading sos debug extension
within Visual Studio from this msdn blog entry,
http://blogs.msdn.com/vijaysk/archive/2007/11/15/sos-your-visual-studio.aspx
Please let me know if my suggestion works for you or not. If you have any
future questions or concerns about this, please feel free to let me know. I
will do my best to follow up.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks!
My issue is resolved.

-
Ashutosh

Hello Ashutosh,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you. We can get the current
Thread's OSID by calling AppDomain.GetCurrentThreadId(). This function will
return the same result as we call the Winows API GetCurrentThreadID() using
PInvoke. But that function is deprecated because it does not provide a
stable Id when managed threads are running on fibers (aka lightweight
threads).

From your description, if getting the OS thread ID is for debugging and
testing purpose, may I suggest to use the SOS debug extension and its
!threads command in Visual Studio instead? This command will print out all
managed threads' statistics information including the OSID. The following
is a typical output for the !threads command and the numbers 5060/5536/5976
are the OS thread IDs:

------------------------------------------------------------------------
!threads
PDB symbol for mscorwks.dll not loaded
ThreadCount: 3
UnstartedThread: 0
BackgroundThread: 1
PendingThread: 0
DeadThread: 0
Hosted Runtime: no
PreEmptive GC Alloc Lock
ID OSID ThreadOBJ State GC Context Domain
Count APT Exception
5060 1 13c4 0058a780 86028 Enabled 01c5a1d8:01c5bfe8 00550810 0
STA
5536 2 15a0 00558628 b228 Enabled 00000000:00000000 00550810 0
MTA (Finalizer)
5976 3 1758 005e37d8 8b028 Enabled 00000000:00000000 00550810 0
MTA
-------------------------------------------------------------------------

You can get the more detailed information about loading sos debug extension
within Visual Studio from this msdn blog entry,
http://blogs.msdn.com/vijaysk/archive/2007/11/15/sos-your-visual-studio.aspx
. Please let me know if my suggestion works for you or not. If you have any
future questions or concerns about this, please feel free to let me know. I
will do my best to follow up.


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top