问题描述
下面是我的code片断:
Here is my code snippet:
PerformanceCounter cpuload = new PerformanceCounter();
cpuload.CategoryName = "Processor";
cpuload.CounterName = "% Processor Time";
cpuload.InstanceName = "_Total";
Console.WriteLine(cpuload.NextValue() + "%");
但输出始终为0%,而cpuload.RawValue就像736861484375左右,发生了什么NextValue()?我的CPU显然是0%的使用率。
But the output is always 0%, while the cpuload.RawValue is like 736861484375 or so, what happened the NextValue()? My cpu is obviously in 0% usage.
谢谢你们〜8 ^)
推荐答案
他的第一次迭代计数器将始终为0,因为它没有任何比较的最后一个值。试试这个:
The first iteration of he counter will always be 0, because it has nothing to compare to the last value. Try this:
var cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total");
Console.WriteLine(cpuload.NextValue() + "%");
Console.WriteLine(cpuload.NextValue() + "%");
Console.WriteLine(cpuload.NextValue() + "%");
Console.WriteLine(cpuload.NextValue() + "%");
Console.WriteLine(cpuload.NextValue() + "%");
这时你应该会看到一些数据出来。它由一个恒定的图表可以看到或更新的场景......这就是为什么你没有过这个问题经常来。
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.
这里的:
该方法nextValue()总是返回
0值在第一次通话。那么你
必须调用此方法的第二
时间。
这篇关于为什么CPU性能计数器报告维持0%的CPU使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!