//use namespace
using System.Diagnostics;
using System.Threading;
private static void PerformanceCounterTest(string CategoryName, string InstanceName, string CounterName)
{
//表示 Windows NT 性能计数器组件
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
while (true)
{
//wait for first second
Thread.Sleep(1000);
float cpuLoad = pc.NextValue();
Console.WriteLine("CPU load = " + cpuLoad + " %.");
}
}
//Test program
PerformanceCounterFTest("Processor", "% Processor Time","_Total");