3

I'm currently doing this:

PerformanceCounter cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total");
cpuUsage.NextValue();
System.Threading.Thread.Sleep(1000);
RV = cpuUsage.NextValue();

I call the function periodically to get CPU usage. When I monitor the system in TaskManager, the CPU usage reported by PerformanceCounter is consistently 15-20% higher than what TaskManager reports (30% in TaskManager = 50% from PerformanceCounter).

Maybe there's documentation that I overlooked, but does anyone have an explanation? Maybe the CPU usage at the instant it checks is higher and task manager reports an average?

jascur2
  • 77
  • 1
  • 7

2 Answers2

5
  new PerformanceCounter("Processor", ...);

You are using the wrong counter if you insist on seeing an exact match with Task Manager or Perfmon. Use "Processor Information" instead of "Processor". The reason these counters show different values is addressed pretty well in this blog post. Which counter is "right" is a question I wouldn't want to touch with a ten-foot pole :)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • No difference between `Processor` and `Processor Information`, my counter value is always half of task manager value. Win 11 x64 i9-12900k. – jjxtra Apr 24 '22 at 14:34
  • 1
    Had to use `new PerformanceCounter("Processor Information", "% Processor Utility", "_Total");` – jjxtra Apr 24 '22 at 14:37
  • 1
    @jjxtra After about an hour of fruitlessly digging through this stuff, your comment worked. For anyone looking, that's the correct performance counter to use if you want something that appears to match the task manager. Now whether the task manager is itself correct is something I'm going to speak to. :) – Ari Roth Jan 25 '23 at 05:20
1

Did not have luck with accepted answer, so adding my comment as an answer to hopefully help people find this through online search a little better.

var counter = new PerformanceCounter("Processor Information", "% Processor Utility", "_Total");
jjxtra
  • 20,415
  • 16
  • 100
  • 140