//根据categoryName ,counterName,instanceName 获得性能情况显示,取5秒内的平均值
private static float CPUPerformance(string CategoryName, string InstanceName, string CounterName)
{
    PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
  
    float cpuload = 0;
    float averageload = 0;
    for (int x = 0; x < 5; x++)
    {
        Thread.Sleep(1000);
        cpuload += pc.NextValue();
    }
    averageload = cpuload / 5;
    return averageload;
}

//CPU使用率
float CPU_Usage = CPUPerformance("Processor", "_Total", "% Processor Time");

获取 CPU 5秒内的平均使用情况