title: Get the Cpu usage of each process from wmi
body: <p>I found many sources to get the cpu usage of each process. in general there are many ways to get the cpu usage of process .</p>
  
  <ol>
  <li>percentprocessortime from win32_perfformatteddata_perfproc_process </li>
  <li>performancecounter class in system.diagnostics</li>
  <li>by manual calculation </li>
  <li>Process class (by process.getcurrentprocess().totalprocessortime;)
  as said in <a href="https://stackoverflow.com/questions/11523150/how-do-you-monitor-the-cpu-utilization-of-a-process-using-powershell">here</a>. </li>
  </ol>
  
  <p><strong>FirstWay:</strong> </p>
  
  <p>For the remote process monitoring(my scenario is remote monitoring), the percentprocessortime always shows value 0 to 100+. this 100+ happens because of multiple processors in a system. it can be calculated by using percentprocessortime/ processorcount.  </p>
  
  <p><strong>Question in firstway:</strong> </p>
  
  <p>i can read the percentprocessortime in wmi explorer, it shows all the values are 0 or 100 only not other than this value. is this value is correct? or is it useful for monitoring the value?</p>
  
  <p><strong>Second Way:</strong></p>
  
  <p>for PerformanceCounter class monitoring, it can be done for local only. so i cannot use this. is it possible to use this for remote?</p>
  
  <p><strong>Third Way:</strong> </p>
  
  <p>(biggest confusion happening here in terms of which formula to use.) this calculation is made either by a PerformanceCounter class or win32_process class from wmi. some says to calculate the performance counter by using the follwing</p>
  
  <p>consider  single CPU and </p>
  
  <p>(processor\%processor time) = 10%</p>
  
  <p>(processor\%user time) = 8%</p>
  
  <p>(processor\% privilege time) = 2%</p>
  
  <p>(process\% processor time\your application) = 80%</p>
  
  <p>You application is using 80% of the (processor\% user time) which is (8*.8)=6.4% of the CPU. </p>
  
  <p>for more refer <a href="http://social.technet.microsoft.com/Forums/en-US/0435e7c5-3cda-41a0-953e-7fa462fde03b/perfmon-process-processor-time-vs-task-managers-cpu-usage-for-monitoring-a-specific-user?forum=perfmon" rel="nofollow noreferrer">here</a>.</p>
  
  <p>by calculating the usermodetime and kernelmodetime from win32_process by using the following formulae</p>
  
  <pre><code>DateTime firstSample, secondSample;
  firstSample = DateTime.Now;
  queryObj.Get();
  //get cpu usage
  ulong u_oldCPU = (ulong)queryObj.Properties["UserModeTime"].Value 
                  +(ulong)queryObj.Properties["KernelModeTime"].Value;
  //sleep to create interval
  System.Threading.Thread.Sleep(1000);
  //refresh object
  secondSample = DateTime.Now;
  queryObj.Get();
  //get new usage
  ulong u_newCPU = (ulong)queryObj.Properties["UserModeTime"].Value
                 + (ulong)queryObj.Properties["KernelModeTime"].Value;
  decimal msPassed = Convert.ToDecimal(
                               (secondSample - firstSample).TotalMilliseconds);
  
  //formula to get CPU ussage
  if (u_newCPU &gt; u_oldCPU)
      PercentProcessorTime = (decimal)((u_newCPU - u_oldCPU) / 
                                 (msPassed * 100 * Environment.ProcessorCount));
  
  Console.WriteLine("Process name " + queryObj.Properties["name"].value);                       
  Console.WriteLine("processor time " + PercentProcessorTime);
  </code></pre>
  
  <p>the above code results output in 85.999 and sometimes 135.89888. i was so confused which way can i calculate the cpu usage of process.</p>
  
  <p><strong>Note:</strong>
  Its a duplicate. I cannot come to the conclusion from the existing sources. and i was confused. so only i asked a question.</p>