PerformanceCounter cpuload = new PerformanceCounter();
cpuload.CategoryName = "Processor";
cpuload.CounterName = "% Processor Time";
cpuload.InstanceName = "_Total";
Console.WriteLine(cpuload.NextValue() + "%");
The output is always 0%, while the cpuload.RawValue
is like 736861484375 or so, what happened at NextValue()
?
First retrieve first value (would be 0)
Then wait for 1000 milisec
Then retrieve second value which is the true cpu usage.
The code should look like this:
The first iteration of he counter will always be 0, because it has nothing to compare to the last value. Try this:
Then you should see some data coming out. It's made to be seen in a constant graph or updated scenario...that's why you don't come across this problem often.
Here's the MSDN reference: