Hi Kevin, thanks for your reply
Your understanding is correct, and your example is exactly one of the
approaches I've tried.
Unforfunately some of the methods that I'll need to measure execute very
quickly, and just calling NextValue() takes on average 0.02s-0.03s, which is
too slow for my needs.
I actually think this is a bit of a dead-end anyway, because from what I can
tell, the resolution of the counter seems to be in the centisecond range
anyway - i.e. when I execute it in quick succession, I get the same values
returned, then the reported values jump up by 0.01s.
As a point of note, I used reflector to have a peek to see what was going on
under the hood of the NextValue() method, and from what I could see it has to
re-create the Counter object, parse all of the instance data into a memory
structure and then find the instance I'm interested in. From the looks of
things this is because when you read a counter you get back all the instance
data in one big chunk of memory. (If you can get at the source code, you
will probably be able to see this in the constructor of
System.Diagnostics.CategorySample)
So, unless there is another way of reading thread timings at a higher
resolution, this may be a no-go!
Thanks,
Mike
"Kevin Yu [MSFT]" wrote:
Hi Mike,
First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the CPU time of a
certain thread. If there is any misunderstanding, please feel free to let
me know.
As far as I know, we can use both methods you mentioned to get the CPU
time. For the PerformanceCounter, we needn't get all the performance
information for all the running threads and processes every time. We can
specify the instance name, category and counter name for the performance
counter object. When we call NextValue method, it only returns the
information we need. Both of the two methods are fast enough on my machine.
Here is an example.
System.Diagnostics.PerformanceCounter pc = new
System.Diagnostics.PerformanceCounter("Thread", "% Processor Time");
pc.InstanceName = "WindowsApplication82/0";
float f = pc.NextValue();
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."