0

I was wondering if anyone could help with getting cpu usage information for a specific application, I am able to get the usage information identical to what you would see in the task manager using the following code

Any help would be greatly appreciated, thanks.

Community
  • 1
  • 1
Chris
  • 915
  • 8
  • 28
  • Use a profiler for performance, not for memory – Gilad Feb 20 '15 at 19:24
  • I have seen code such as: PerformanceCounter myAppCpu = new PerformanceCounter( "Process", "% Processor Time", "app", true); double pct = myAppCpu.NextValue(); However this gives one value for the whole cpu and not % per core which is what i am after. – Chris Feb 20 '15 at 19:35
  • possible duplicate of [How can I get CPU load per core in C#?](http://stackoverflow.com/questions/2938629/how-can-i-get-cpu-load-per-core-in-c) – rducom Feb 20 '15 at 20:12
  • Chris a profiler is a software to analyze code, not a code to use. – Gilad Feb 20 '15 at 21:10
  • I know what a profiler is, but i need the cpu information for my program, not for debugging purposes. – Chris Feb 21 '15 at 16:12
  • @Sharped - this is not a duplicate, perhaps my poor grammar didn't help but I am after cpu information specific to my application – Chris Feb 21 '15 at 16:18

2 Answers2

0

There are two options to do this.

First one is to use WMI

    var searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
    var cpuUsages = searcher.Get()
        .Cast<ManagementObject>()
        .Select(x => new
                      {
                          Name = x["Name"],
                          Usage = x["PercentProcessorTime"]
                      }
        )
        .ToList();

    var totalUsage = cpuUsages.Where(x => x.Name.ToString() == "_Total").Select(x => x.Usage).SingleOrDefault();

The second is to use a PerformanceCounter foreach core.

    int processorCores = new ManagementObjectSearcher("Select * from Win32_Processor").Get().Cast<ManagementBaseObject>().Sum(item => int.Parse(item["NumberOfCores"].ToString()));
    var performanceCounter = new PerformanceCounter[processorCores];
    for (var i = 0; i < processorCores; i++)
    {
        performanceCounter[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
    }
  • Correct me if i am mistaken but both of these code samples will give general cpu information (not application specific) across all cores – Chris Feb 21 '15 at 16:14
0

Many years ago I used the pdh commands to get various per process information (in my case, CPU, idle and power states). I believe they are now legacy and there is a newer updated set of APIs.

I found these examples of using performance information from the MSDN website. I haven't tried them.

One note: The names of the counters can be deceptive. Many times, I found that the counter name changed slightly from one generation of windows to the next. As a made up example, "COUNTERINFOPROCESS" might become "COUNTERINFPROCESS". There is also an API for dumping a list of the names of all counters.

Taylor Kidd
  • 1,463
  • 1
  • 9
  • 11