14

To return the CPU usage by using WMI, do I return the PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor?

If not, which class should I look towards? Thanks.

jgstew
  • 131
  • 2
  • 9
jpavlov
  • 2,215
  • 9
  • 43
  • 58
  • Try looking [here at this question](http://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c). It should answer your question – Icemanind Mar 19 '12 at 20:50
  • Yes the [Win32_PerfFormattedData_PerfOS_Processor](http://msdn.microsoft.com/en-us/library/windows/desktop/aa394271%28v=vs.85%29.aspx) WMI class is the right for get the CPU usage. – RRUZ Mar 19 '12 at 20:52

3 Answers3

23
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
foreach (ManagementObject obj in searcher.Get())
{
    var usage = obj["PercentProcessorTime"];
    var name = obj["Name"];
    Console.WriteLine(name +" : " + usage);
}

And for Linq lovers

ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
var cpuTimes = searcher.Get()
    .Cast<ManagementObject>()
    .Select(mo => new
        {
            Name = mo["Name"],
            Usage = mo["PercentProcessorTime"]
        }
    )
    .ToArray();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • Thanks, that is pretty much how I set mine up. – jpavlov Mar 20 '12 at 12:14
  • 2
    Can also do: new ManagementObjectSearcher("SELECT PercentProcessorTime FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name='_Total'").Get().Cast().First().Properties["PercentProcessorTime"].Value.ToString(); .Properties["PercentProcessorTime"] .Value.ToString() – Sevin7 Sep 07 '14 at 11:06
6

It seems like the info is also available in WMI here:

select LoadPercentage from Win32_Processor

"Load capacity of each processor, averaged to the last second. Processor loading refers to the total computing burden for each processor at one time."

OR:

select LoadPercentage from CIM_Processor

"Loading of the processor, averaged over the last minute, in a percentage."

OR:

select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor also seems to work.


Note: these often return multiple results per CPU core and have to be summed/averaged to get the total CPU usage for the system as a whole, so look for that.

This question and answer really has more to do with WMI since getting info from WMI with C# is really a different question and should be very similar for any WMI query in C#.

jgstew
  • 131
  • 2
  • 9
0

I think below will give full CPU usage

ObjectQuery objQuery = new ObjectQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name=\"_Total\"");
    ManagementObjectSearcher mngObjSearch = new ManagementObjectSearcher(CommonSettings.MngScope, objQuery);
    ManagementObjectCollection mngObjColl = mngObjSearch.Get();
    
    if (mngObjColl.Count > 0)
    {
        foreach (ManagementObject mngObject in mngObjColl)
        {
            try
            {
                 uint cpu_usage = 100 - Convert.ToUInt32(mngObject["PercentIdleTime"]);
                 break;
            }
            catch (Exception ex)
            {
                 break;
            }
        }
     }
Krish
  • 11
  • 1