Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/06b215f1-c6a6-4ebb-ba4e-d17484308741/" rel="noreferrer">this</a> post:</p> <p><strong>To get the entire PC CPU and Memory usage:</strong></p> <pre><code>using System.Diagnostics; </code></pre> <p>Then declare globally: </p> <pre><code>private PerformanceCounter theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); </code></pre> <p>Then to get the CPU time, simply call the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx" rel="noreferrer"><code>NextValue()</code></a> method:</p> <pre><code>this.theCPUCounter.NextValue(); </code></pre> <p>This will get you the CPU usage</p> <p>As for memory usage, same thing applies I believe: </p> <pre><code>private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes"); </code></pre> <p>Then to get the memory usage, simply call the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx" rel="noreferrer"><code>NextValue()</code></a> method:</p> <pre><code>this.theMemCounter.NextValue(); </code></pre> <p><strong>For a specific process CPU and Memory usage:</strong></p> <pre><code>private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName); </code></pre> <p>where <code>Process.GetCurrentProcess().ProcessName</code> is the process name you wish to get the information about.</p> <pre><code>private PerformanceCounter theMemCounter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName); </code></pre> <p>where <code>Process.GetCurrentProcess().ProcessName</code> is the process name you wish to get the information about.</p> <p>Note that <strong>Working Set may not be sufficient in its own right to determine the process' memory footprint</strong> -- see <a href="https://stackoverflow.com/q/1984186/389828">What is private bytes, virtual bytes, working set?</a></p> <p><strong>To retrieve all Categories, see <a href="http://msdn.microsoft.com/en-us/library/2fh4x1xb(vs.71).aspx" rel="noreferrer">Walkthrough: Retrieving Categories and Counters</a></strong></p> <p>The difference between <code>Processor\% Processor Time</code> and <code>Process\% Processor Time</code> is <code>Processor</code> is from the PC itself and <code>Process</code> is per individual process. So the processor time of the processor would be usage on the PC. Processor time of a process would be the specified processes usage. For full description of category names: <a href="http://technet.microsoft.com/en-us/library/cc768048.aspx" rel="noreferrer">Performance Monitor Counters</a></p> <p><strong>An alternative to using the Performance Counter</strong></p> <p>Use <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.totalprocessortime.aspx" rel="noreferrer">System.Diagnostics.Process.TotalProcessorTime</a> and <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.totalprocessortime.aspx" rel="noreferrer">System.Diagnostics.ProcessThread.TotalProcessorTime</a> properties to calculate your processor usage as this <a href="http://www.codeproject.com/KB/system/processescpuusage.aspx" rel="noreferrer">article</a> describes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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