为什么 cpu 性能计数器一直报告 0% cpu 使用率?
PerformanceCounter cpuload = new PerformanceCounter();
cpuload.CategoryName = "Processor";
cpuload.CounterName = "% Processor Time";
cpuload.InstanceName = "_Total";
Console.WriteLine(cpuload.NextValue() + "%");
输出始终为 0%,而 cpuload.RawValue
类似于 736861484375 左右,NextValue()
发生了什么?
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()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
计数器的第一次迭代将始终为 0,因为它没有任何内容可与最后一个值进行比较。试试这个:
然后你应该看到一些数据出来。它是为了在恒定的图表或更新的场景中看到的......这就是为什么你不会经常遇到这个问题。
以下是 MSDN 参考:
The first iteration of the 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:
首先检索第一个值(将为 0)
,然后等待 1000 毫秒,
然后检索第二个值,即真实的 cpu 使用情况。
代码应该如下所示:
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: