为什么CPU性能计数器报告维持0%的CPU使用率? [英] Why the cpu performance counter kept reporting 0% cpu usage?

查看:366
本文介绍了为什么CPU性能计数器报告维持0%的CPU使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的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.

这里的 MSDN参考

该方法nextValue()总是返回
  0值在第一次通话。那么你
  必须调用此方法的第二
  时间。

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

这篇关于为什么CPU性能计数器报告维持0%的CPU使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆