Instance name from Thread Id?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Instance name from Thread Id?

  1. #1
    Join Date
    Aug 2003
    Posts
    213

    Instance name from Thread Id?

    Hi,

    I'm using pdh to generate Performace data for a thread in my multithreaded program.
    The counter should be like
    "\\Thread(App/3)\\% Processor Time"
    here App/3 is the name of the instance for the thread.

    My question is:
    I know the thread Id for my thread, how do I find the instance name of the thread from my program?

    Please help

  2. #2
    Join Date
    Aug 2003
    Posts
    213

    Unhappy

    Help please

  3. #3
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    There are a number of ways to accomplish this. A generic way would be to call GetModuleFileName(...)/GetModuleFileNameEx(...)

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Take a look at the following FAQ...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  5. #5
    Join Date
    Aug 2003
    Posts
    213
    correct me if I'm wrong, I think that program will return the path of the application. I don't need that, I need the name of the Thread for which I have the handle.

    Please advise

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    As far as Windows is concerned, a thread doesn't have a name. it is just the address of the ThreadProc !!!

    What is the purpose for wanting to know the "name". If you explain that maybe someone here will provide a solution.

  7. #7
    Join Date
    Aug 2003
    Posts
    213
    Please look at my first post in this thread, I need the name of the thread to creat a counter for the performance monitoring using pdh.

    In my application I have around 20 thread and in the perfmon they are listed as
    App/0
    App/1
    App/2
    ..
    App/20

    etc, where App is the name of the application.
    Now I want to find the %CPU for only one thread and I just know the handle for it.

    Please advise.

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Doesn't the performance monitor show the thread ID ? That is more what you need.

    You could probably log the thread ID of each thread you create in the thread proc using GetCurrentThreadID()

  9. #9
    Join Date
    Aug 2003
    Posts
    213
    Please forgive my ignorance, but
    How to get the thread name from thread id?

    I definitely need the name of the thread for the counter as the counters are like:

    "\\Thread(App/3)\\% Processor Time"

    where App/3 is the thread name.

  10. #10
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Note that the name you see in the performance counter is not a thread name. It is just a friendly name that performance monitor uses for display. What is more important is the thread ID.

    Given a thread, you can find the thread ID by adding a counter.

    1. Select the performance object as thread. Now, you see a list of the processes and their threads in the list.
    2. In the Select Counters from list, select ID Thread.
    3. In the select instances from list, select the thread you are interested in.

    In your code add the GetCurrentThreadID in each thread and trace out the thread ID. Compare the 2.

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    the name you are seeing is the PROCESS name.


    Thread(App/3)

    Thread 3, in Process App.

    So again...GetModuleFileName(...)/GetModuleFileNameEx(...)

    If you need the order of thread creation use Thread32First(...) in the tool help library. Or use the Native Api via NtQuerySystemInformation(...). Or WMI. The performance monitor should have a counter to enum the threads also.
    Last edited by Mick; June 17th, 2004 at 04:02 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center