Search code examples
c#.netthreadpool

how to get ProcessThread.TotalProcessorTime of a current thread


ok, I am using ThreadPool to start threads. Inside threaad code I'm trying to figure out hom much cpu time it actually used. I've read there is ProcessThread . TotalProcessorTime property for this (http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.totalprocessortime.aspx) but I just can't get to read it. So how do I get it for a current thread?


Solution

  • A few points:

    1. To get the correct process thread via ProcessThread.TotalProcessorTime you'll need to know the native thread ID.

    2. It sounds very much like you are comparing the managed thread ID (via Thread.CurrentThread.Name) with the native thread ID to get the current thread within the via the System.Diagnostics namespace. Remember that managed thread ID != ProcessThreadID. You'll need to use the GetCurrentThreadId function

    3. Thread pool threads can be recycled, so the TotalProcessorTime for a thread pool may be larger than what you are expecting

    4. If you are profiling thread usage it may be easier to use the thread profiler in Visual Studio Ultimate, or another good profiler (e.g. ANTS, DotTrace, SciTech, etc.)