How to get the CPU Usage in C#?

后端 未结 10 1981
醉酒成梦
醉酒成梦 2020-11-22 06:22

I want to get the overall total CPU usage for an application in C#. I\'ve found many ways to dig into the properties of processes, but I only want the CPU usage of the proce

10条回答
  •  感情败类
    2020-11-22 07:02

    This seems to work for me, an example for waiting until the processor reaches a certain percentage

    var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    int usage = (int) cpuCounter.NextValue();
    while (usage == 0 || usage > 80)
    {
         Thread.Sleep(250);
         usage = (int)cpuCounter.NextValue();
    }
    

提交回复
热议问题