title: How to calculate % CPU Utilization using PerformanceCounter Class?
body: <p>I been looking at <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter" rel="nofollow noreferrer">PerformanceCounter Class</a> for monitoring system performance. I have figured out how to call built in counters. What I'm struggling with is understanding the values that I get back and how compare those values to what I can see in Task Manager.</p>
  
  <p>So far I have successfully been able to monitor available RAM in MB and it correctly corresponded to value in Task Manager.</p>
  
  <p>first I created PerformanceCounter</p>
  
  <pre><code> myMemoryCounter = new PerformanceCounter("Memory", "Available MBytes", true);
  </code></pre>
  
  <p>Then I put the following line inside a loop.</p>
  
  <pre><code>Console.WriteLine("Value" + myMemoryCounter.NextSample().RawValue.ToString());
  </code></pre>
  
  <p>When looking at counters for processor, I'm not able to make connection to % CPU Utilization that can be observed in Task Manager.</p>
  
  <p>Do I need to have multiple counters and compute the value or is there easier way?</p>
  
  <p>Update:</p>
  
  <p>Looking at question <a href="https://stackoverflow.com/questions/4679962/what-is-the-correct-performance-counter-to-get-cpu-and-memory-usage-of-a-process">What is the correct Performance Counter to get CPU and Memory Usage of a Process?</a> it just describes which counter to use but not how to convert results to percentage?</p>
  
  <p><code>NextSample().RawValue</code> returns long data type and I need to know how to convert it to percentage.</p>
  
  <p>Using the following code</p>
  
  <pre><code> var _myCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
   Console.WriteLine("CPU " + _myCPUCounter.NextSample().RawValue.ToString("0.000"));
  </code></pre>
  
  <p>I get this value  <code>1794607539062</code> how do I convert that to percentage?</p>