C#获取cpu计数
private static void PerformanceCounterFun(string CategoryName, string InstanceName, string CounterName)
{
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
while (true)
{
Thread.Sleep(1000);
float cpuLoad = pc.NextValue();
Console.WriteLine("CPU load = " + cpuLoad + " %.");
}
}
PS
performance counter在计算值得时候,需要两个样本,如果我们获取到PerformanceCounter后直接调用NextValue()方法,则只会获取到第一个样本的值,该值往往会是0。
pc.NextValue()
System.Threading.Thread.Sleep(1000)
return pc.NextValue()
参考http://blog.csdn.net/jiangxinyu/article/details/5480401提供