gpt4 book ai didi

c# - PerformanceCounter CPU 使用率值始终为 0

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:08 33 4
gpt4 key购买 nike

对于以下性能计数器,我总是得到 % Processor Time is 0,甚至我的 CPU 显示 100% 使用率,可能是什么原因?

PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
Console.WriteLine(pc.NextValue());

最佳答案

NextValue() documentation 中的注释对此进行了解释:

If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.

因此,如果您将代码更改为:

while (true)
{
Console.WriteLine(pc.NextValue());
Thread.Sleep(1000);
}

...然后您会看到适当的值。

关于c# - PerformanceCounter CPU 使用率值始终为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44280749/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com