Calculate total CPU usage

前端 未结 7 550
感动是毒
感动是毒 2020-12-18 15:55

What is best algorithm for calculating total CPU usage at a particular time during execution of a process.

I am working windows platform in C++.

7条回答
  •  孤城傲影
    2020-12-18 16:27

    // Declare
    PerformanceCounter cpuCounter = null;

    // initialize somewhere in the constructor..
    cpuCounter = new PerformanceCounter();
    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    Then to get the CPU usage in % just call
    cpuCounter.NextValue()

    Source:http://zamov.online.fr/EXHTML/CSharp/CSharp_927308.html

提交回复
热议问题