title: Does a while(true) Infinite Loop Peg the CPU?
body: <p>I have a situation where I need a constant loop running in a service, each time checking certain conditions, and then taking action as appropriate.  From a design perspective, using a <code>while (true)</code> loop fits perfectly:</p>
  
  <pre><code>while (true)
  {
      Process();
  }
  </code></pre>
  
  <p>The Process() method checks the state of an in-memory (fast) data store.  If it finds there is work to do based on the state, it Processes it using <code>Task.Run()</code> (so control returns to the loop immediately).</p>
  
  <p>I have seen some references that this will eat up unnecessary CPU cycles, and that I should use a <code>Timer</code>, or add <code>Thread.Sleep</code> to the loop.  I have also seen posts stating that there is nothing wrong with using <code>while (true)</code>.</p>
  
  <p>On my dev machine, it does not seem to have a negative impact, although the CPU usage does go up.  It may be that the CPU usage gets spread across multiple cores, where the deployment environment may only have a single core.</p>
  
  <p>Does a <code>while (true)</code> loop have a negative performance impact on the CPU, and if so, what is the correct way to mitigate the impact?</p>