title: Infinite While Loop in C#, CPU usage 100%
body: <p>I have a web response stream which produces data intermittently. There is no way of knowing when will data be received on this channel.</p>
  
  <p>In order to read all data in my application, I use a while(true) which results in 100% CPU usage.</p>
  
  <p>I cannot use ManualResetEvent as the application has to keep reading from the stream all the time. This works OK as long as data is received but when there is no data in stream, other threads cannot get enough CPU time to process.
  My code looks like:-</p>
  
  <pre><code>StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream());
  while(true)
  {
      int charvalue = streamReader.Read();
      // More code to process data read above
  }
  </code></pre>
  
  <p>I don't want to use Thread.Sleep() as it slows my application unnecessarily and still want other threads to process when there is no data received on this thread.</p>