none
What is the relation between ICorProfilerCallback::ThreadAssignedToOSThread manageThreadId and Thread.ManagedThreadId Property ? RRS feed

  • Question

  • I am trying to find the mapping between the ManageThreadId reported by the ICorProfilerCallback::ThreadAssignedToOSThread API and that which I can obtain from the .NET managed API Thread.ManagedThreadId Property.

    In my tests these ids have completely different values. How can I map a thread from my managed code to my profiler API ? The MSDN documentation warns that I should not use the OS thread id as this could change due to .NET implementation of managed threads on fibers. 

    Any responses and discussions on this are welcome

    Regards,

    Sanjay



    Sanjay Mehta

    Thursday, January 17, 2013 12:25 AM

Answers

  • Hi, Sanjay.

    The profiling API does not provide a dedicated method to convert between ThreadID and the ID exposed by the managed thread object.   If your goal is to simply annotate threads with something recognizeable to your user, consider using the thread's name (see ICorProfilerCallback2::ThreadNameChanged()).

    Another option would be for you to rewrite IL dynamically, in which case you could, from managed code, grab Thread.ManagedThreadId and hand it to your profiler (via P/Invoke), which could call ICorProfilerInfo::GetCurrentThreadID and correlate the two.

    Thanks,
    Dave

    Monday, January 28, 2013 5:24 PM

All replies

  • Additionally, the MSDN documentation states 

    The ThreadAssignedToOSThread callback exists so that the profiler can maintain an accurate mapping across fibers of operating-system threads to managed threads.


    Sanjay Mehta

    Thursday, January 17, 2013 12:56 AM
  • Hi Sanjay,

    Welcome to the MSDN Forum.

    The method ThreadAssignedToOSThread take two in parameters:

    HRESULT ThreadAssignedToOSThread(
        [in] ThreadID managedThreadId,
        [in] DWORD    osThreadId);

    So how do you compare with the parameter managedThread with the Thread.ManagedId?

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 18, 2013 6:52 AM
    Moderator
  • True, the  method ThreadAssignedToOSThread take two in parameters: But this is a profiler API "callback" function. The registered profiler is notified by the CLR by this "callback" function and hence values filled in by the CLR.  see MSDN documentation http://msdn.microsoft.com/en-us/library/ms233475.aspx Notifies the profiler that a managed thread is being implemented using a particular operating system thread.

    Regards,

    Sanjay


    Sanjay Mehta

    Friday, January 18, 2013 6:29 PM
  • Hi Sanjay,

    I am trying to involve some other one into this case, it will take some time, wait it patiently, thank you.

    And would you like to post how do you implement this?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, January 21, 2013 2:00 AM
    Moderator
  • Hi, Sanjay.

    The profiling API does not provide a dedicated method to convert between ThreadID and the ID exposed by the managed thread object.   If your goal is to simply annotate threads with something recognizeable to your user, consider using the thread's name (see ICorProfilerCallback2::ThreadNameChanged()).

    Another option would be for you to rewrite IL dynamically, in which case you could, from managed code, grab Thread.ManagedThreadId and hand it to your profiler (via P/Invoke), which could call ICorProfilerInfo::GetCurrentThreadID and correlate the two.

    Thanks,
    Dave

    Monday, January 28, 2013 5:24 PM