[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

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

Ashutosh

9/29/2008 6:40:00 PM

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
4 Answers

Peter Duniho

9/29/2008 7:02:00 PM

0

On Mon, 29 Sep 2008 11:39:53 -0700, Ashutosh <smbs-msdn@nospam.nospam>
wrote:

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

Yes. But since there's no guarantee that a managed thread is exactly the
same as an unmanaged thread, you should think twice before writing code
that relies on that.

Ashutosh

9/30/2008 3:06:00 AM

0

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

Peter Duniho wrote:
> On Mon, 29 Sep 2008 11:39:53 -0700, Ashutosh <smbs-msdn@nospam.nospam>
> wrote:
>
>> 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??
>
> Yes. But since there's no guarantee that a managed thread is exactly
> the same as an unmanaged thread, you should think twice before writing
> code that relies on that.

v-jzho

9/30/2008 6:17:00 AM

0

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-s...
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 (v-jzho@online.microsoft.com, 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:
msdnmg@microsoft.com.

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

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&am....
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Ashutosh

10/3/2008 3:20:00 AM

0

Thanks!
My issue is resolved.

-
Ashutosh

v-jzho@online.microsoft.com wrote:
> 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-s...
> . 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 (v-jzho@online.microsoft.com, 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:
> msdnmg@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....
>
> 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&am....
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>