Note that there are some explanatory texts on larger screens.

plurals
  1. POWrong calculated CPU usage using C# and WMI
    text
    copied!<p>This is my function for enumerating processes on windows box and calculating percentage of CPU usage for each process but results are not correct.</p> <p>CPU usage does't add up to 100% but more like to 120% or 130% and I don't know what I'm doing wrong. It seems like it calculats right CPU usage for varoius apps like firefox, VS2010, office,.. but has problems with System Idle Process. </p> <pre><code>public List&lt;ProcInfo&gt; GetRunningProcesses() { List&lt;ProcInfo&gt; allProcesses = new List&lt;ProcInfo&gt;(); UInt64 currentProcessCpuTime = 0; UInt64 allProcessCpuTime = 0; SelectQuery wmiQuery = new SelectQuery("SELECT Name, Description, ProcessId, KernelModeTime, UserModeTime FROM Win32_Process"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(connectionScope, wmiQuery); ManagementObjectCollection moc = oSearcher.Get(); foreach (ManagementObject mo in moc) { allProcessCpuTime += (UInt64)mo["KernelModeTime"] + (UInt64)mo["UserModeTime"]; } foreach (ManagementObject mo in moc) { currentProcessCpuTime = (UInt64)mo["KernelModeTime"] + (UInt64)mo["UserModeTime"]; allProcesses.Add(new ProcInfo((string)mo["Name"], (string)mo["Description"], (UInt32)mo["ProcessId"], (currentProcessCpuTime / (double)allProcessCpuTime * 100)); } return allProcesses; } </code></pre> <p><strong>EDIT:</strong></p> <p>I found that my function is all wrong. </p> <p>I'm starting a bounty for the best working solution. Solution needs to work for local and remote system and should be fast. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload