Reputation: 27673
I have the following code:
PerformanceCounter c = new PerformanceCounter("Processor", "% Processor Time", "_Total");
And then I use it with: c.NextValue()
What I get is around twice as high as Task Manager manager shows as the cpu utilization. Though my cpu has 2 cores, as far as I know that's not supposed to make a difference. So why is this happening? Or am I wrong about the dual core causing it? (Though searching for some time, I can't find any documentation for a list of performance counters.)
EDIT
As suggested in a comment, I tried perfmon (Performance Monitor) and it shows the same result I get. Why the discrepancy?
Upvotes: 0
Views: 3349
Reputation: 2826
If you use this, you can get the same result with the task manager :
cpuCounter = new PerformanceCounter(
"Processor Information",
"% Processor Utility",
"_Total",
true
);
Upvotes: 4
Reputation: 5953
According to this blog you should use "Processor Information" instead of "Processor" if you want to match the exact value of the task manager. Question is what value is right, but that is a design decision I guess.
For someone performing performance testing and analysis, the ability to log CPU utilization data over time is critical. A data collector set can be configured via logman.exe to log the “% Processor Time”counter in the “Processor Information” object for this purpose.
PerformanceCounter c = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
Should do it.
Upvotes: 2