Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd recommend OS functionality. There are performance counters and WinAPI functions for this on Windows.</p> <p>Here is an example using <strong>performance counters</strong> from <a href="http://blogs.msdn.com/bclteam/archive/2006/06/06/619284.aspx" rel="nofollow noreferrer">BCL Team Blog</a>:</p> <pre><code>foreach (Process proc in Process.GetProcesses()) { using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", proc.ProcessName)) { pcProcess.NextValue(); System.Threading.Thread.Sleep(1000); Console.WriteLine("Process:{0} CPU% {1}", proc.ProcessName, pcProcess.NextValue()); } } </code></pre> <p>This code makes the same <strong>with WMI</strong> from <a href="http://www.codeproject.com/KB/cs/perfgrid.aspx" rel="nofollow noreferrer">CodeProject</a>:</p> <pre><code>public string GetCPU() { decimal PercentProcessorTime=0; mObject_CPU.Get(); ulong u_newCPU = (ulong)mObject_CPU.Properties["PercentProcessorTime"].Value; ulong u_newNano = (ulong)mObject_CPU.Properties["TimeStamp_Sys100NS"].Value; decimal d_newCPU = Convert.ToDecimal(u_newCPU); decimal d_newNano = Convert.ToDecimal(u_newNano); decimal d_oldCPU = Convert.ToDecimal(u_oldCPU); decimal d_oldNano = Convert.ToDecimal(u_oldNano); // Thanks to MSDN for giving me this formula ! PercentProcessorTime = (1 - ((d_newCPU-d_oldCPU)/(d_newNano - d_oldNano)))*100m; // Save the values for the next run u_oldCPU = u_newCPU; u_oldNano = u_newNano; return PercentProcessorTime.ToString("N",nfi);; } </code></pre> <p>So you can query these OS providers (or others for your OS) and sleep your thread if processor utilization is high.</p>
    singulars
    1. This table or related slice is empty.
    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. 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