Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Processors and PerformanceCounter C#

I'm trying to figure out how to gather the current usage percentage of each individual processor on my computer. If I use "System.Environment.ProcessorCount;" I can get the number of processors on my computer and it currently returns "2". I either don't know what I'm looking for or there isn't very much info about this on the internet.

The following is the code I'm currently using to get the total current usage percentage of all processors combined.

protected PerformanceCounter cpuCounter = new PerformanceCounter("processor", "% Processor Time", "_Total");
public string getCurrentCpuUsage()
{
    return cpuCounter.NextValue() + "%";
}

Thank you for any help,

Aaron

like image 208
Aaron Salazar Avatar asked Feb 09 '10 21:02

Aaron Salazar


2 Answers

For the first processor, use

protected PerformanceCounter cpuCounter = 
  new PerformanceCounter("processor", "% Processor Time", "0");

And so on, up to (Environment.ProcessorCount-1).ToString()

like image 156
Henk Holterman Avatar answered Nov 14 '22 14:11

Henk Holterman


Since I left my original question I happened to find the Windows Performance Monitor (C:\Windows\system32\perfmon.msc) on my Windows 7 computer. If one right-clicks the graph on the main window and chooses "Add Counters" then a list of possible strings to use as parameters in the PerformanceCounter is displayed for just about everything one would want to monitor.

like image 5
Aaron Salazar Avatar answered Nov 14 '22 12:11

Aaron Salazar