我已经阅读了一些其他CPU使用问题,但我坚持为什么我的代码不工作。CPU使用问题
我在From1的名为“ComputerPerformance”的单独类中有代码。
class
{
private string cCpuUtilization;
public void cpuUtilization()
{
try
{
PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cCpuUtilization = cpuCounter.NextValue() + "%";
}
catch(System.InvalidOperationException e)
{
}
}
public String getCPUUtilization()
{
return cCpuUtilization;
}
}
的代码运行正常,但我得到什么作为输出...
更新
现在,该代码如下所示:
PerformanceCounter cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Process";
cpuCounter.CounterName = "Private Bytes";
cpuCounter.InstanceName = "Explorer";
cCpuUtilization = cpuCounter.NextValue().ToString() + "%";
我的问题现在它是返回值:6.728499E + 07%... 任何想法,为什么会这样?
摆脱那个try/catch块,也许你会发现什么是错的。 –
我刚刚这样做,它出现了错误: 在System.dll中发生未处理的类型'System.InvalidOperationException异常' 其他信息:由于缺少CounterName而未能初始化。 不知道这是什么意思... –
这意味着“CounterName”属性没有设置。也许看看文档。并且不要隐藏自己的例外。 –