title: cpu usage increasing up to 100% in infinite loop in thread
body: <p>I am implementing a web based chat platform in ASP.NET Web Application, and I use technique similar to long polling. I mean I keep each web request from client for a specific time period(timeout) or until new message arrives, and then response is sent to the client.</p>
  
  <p>I keep connected clients in memory(dictionary object) and when ever new message is sent to a client, I write this message into receiver client's messages array. 
  Client needs to send a request to get his own messages, and I keep this request in an array in memory.</p>
  
  <p>I am using asynchronous http handler for listenings client request, I am keeping web requests in an array in memory. I use threads to check for new messages continously from memory (in dictionary which is created for each client). </p>
  
  <p>I do not use .net thread pool threads to check for new messages or timed out web requests.I create threads like this:</p>
  
  <pre><code>System.Threading.Thread t = new Thread(new ThreadStart(QueueCometWaitRequest_WaitCallback));
  t.IsBackground = false;
  t.Start();
  </code></pre>
  
  <p>In each thread's QueueCometWaitRequest_WaitCallback method I am in an infinite while loop:</p>
  
  <pre><code>while (true)
  {
  ...
  Thread.Sleep(100);
  }
  </code></pre>
  
  <p>In this method, I am checking for web request time out or new message for each Web Request which is also kept in an array in memory.</p>
  
  <p>Everything was working good until I noticed that CPU usage is reaching up to 100% in time. (in minutes after the first connected client)
  At the beginning of first request everything seems to be normal, I mean the CPU usage is not higher than 10% while returning a response to the client. But in time even with 2 clients the CPU usage is increasing up to 100%. It seems CPU usage is 100% only when writing to a response for a client request. If no client is left then everything return to a normal (CPU usage is about 0%) until new web request is done by a client.</p>
  
  <p>I don't know the threads in detail, but I am suspicious about the new threads which I created and works infinitely. It is like operating system gives them more CPU usage and resource in time since they are working all the time, and this Thread.Sleep(100) is not working.</p>
  
  <p>Here is the QueueCometWaitRequest_WaitCallback() method:</p>
  
  <pre><code>void QueueCometWaitRequest_WaitCallback()
  {
     while (true)
     {
        if (processRequest.Length == 0)
        {
            Thread.Sleep(100);
        }
        else
        {
            for (int i = 0; i &lt; processRequest.Length; i++)
            {
                 Thread.Sleep(100);
  
                 // below I am checking for new message or request time out 
                 .................
                 .................
  
                 // If new message or time out I write to response
            }
        }    
     }
  }
  </code></pre>
  
  <p>I hope I could explain the situation, and I am open to any suggestion as well (like implementing in a different way)</p>
  
  <p>If you can help me with this problem I will appreciate gratefully,
  Thanks</p>