How to get the CPU Usage in C#?

后端 未结 10 2204
醉酒成梦
醉酒成梦 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 06:54

    public int GetCpuUsage()
    {
        var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", Environment.MachineName);
        cpuCounter.NextValue();
        System.Threading.Thread.Sleep(1000); //This avoid that answer always 0
        return (int)cpuCounter.NextValue();
    }
    

    Original information in this link https://gavindraper.com/2011/03/01/retrieving-accurate-cpu-usage-in-c/

提交回复
热议问题