【发布时间】:2013-11-27 22:04:58
【问题描述】:
这里有几个关于如何监控 CPU 使用率的问题,但我无法让我的代码显示除 0 以外的任何内容。
有人可以看看,让我知道我做错了什么吗?
PerformanceCounter perform = new PerformanceCounter("Processor", "% Processor Time", "_Total");
public string cpuTime()
{
return perform.NextValue() + "%";
}
public void cpuUtilization()
{
}
public String getCPUUtilization()
{
return cpuTime();
}
【问题讨论】:
-
您是否不止一次调用它?
perform.NextValue()需要两次调用。它给你的数字是number of cycles executed by process between calls/total cycles executed between calls的结果。第一次调用它时,它会给你0,然后它开始给你准确的数字。请参阅this old answer of mine 了解更多信息 -
另请注意,两次调用之间至少需要 100 毫秒才能使其工作,否则您将只能获得 0% 或 100% 的结果。
标签: c# monitoring cpu-usage