gpt4 book ai didi

c# - 为什么 cpu 性能计数器一直报告 0% cpu 使用率?

转载 作者:IT王子 更新时间:2023-10-29 04:09:11 31 4
gpt4 key购买 nike

PerformanceCounter cpuload = new PerformanceCounter();
cpuload.CategoryName = "Processor";
cpuload.CounterName = "% Processor Time";
cpuload.InstanceName = "_Total";
Console.WriteLine(cpuload.NextValue() + "%");

输出始终为 0%,而 cpuload.RawValue 为 736861484375 左右,NextValue() 发生了什么?

最佳答案

计数器的第一次迭代将始终为 0,因为它与最后一个值没有可比较的。试试这个:

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() + "%");

然后你应该会看到一些数据出来了。它可以在常量图表或更新的场景中看到...这就是为什么您不会经常遇到此问题的原因。

这是 MSDN reference :

The method nextValue() always returns a 0 value on the first call. So you have to call this method a second time.

关于c# - 为什么 cpu 性能计数器一直报告 0% cpu 使用率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2181828/

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