title: c# high CPU load on HTTP requests
body: <p>I have a bit of code that is responsible for doing http requests, there can be hundreds of threads per application that are active at a time.</p>
  
  <p>Normally this works fine, however, when there are connection issues(at least I presume this to be the case) the code seems to use an excessive amount of CPU, upto 100%.</p>
  
  <p>This is the piece of code in question:</p>
  
  <pre><code> while (true)
              {
                  try
                  {
                      while (threadNr &gt; newViewerCount || threadNr &gt;= (proxies.Count))
                      {
                          Thread.Sleep(3000);
                      }
  
                      int sleepTimer = 4400 - (int)elapsed;
                      if (sleepTimer &lt; 0)
                      {
  
                          sleepTimer = 1000;
                      }
  
                      Thread.Sleep(sleepTimer);
  
                      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rc.url);
  
                      request.Method = "GET";
                      request.UserAgent = agent;
                      request.Proxy = new WebProxy(proxy);
                      request.Timeout = 5000;
  
                      request.CookieContainer = ck;
                      request.Headers.Add("Accept-Language", "en,en-US;q=0.7,en;q=0.3");
                      request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                      request.Headers.Add("Accept-Encoding", "gzip, deflate");
                      request.UserAgent = userAgents[3];
  
                      HttpWebResponse webresponse;
                      using (webresponse = (HttpWebResponse)request.GetResponse())
                      {
  
                      }
  
                  }
                  catch (Exception e)
                  {
                      int sl = 1500;
                      if (e.Message.Contains("timed"))
                      {
                          elapsed = 4399;
                          sl = 0;
                      }
                      Thread.Sleep(sl);
                      Console.WriteLine("Download exception:" + e.Message);
  
                      if (e.Message.Contains("404"))
                      {
                          Thread.Sleep(3000);
                          rc = getTokenUrl(proxy, ck); 
                      }
  
                      if (e.Message.Contains("forbidden"))
                      {
                          rc = getTokenUrl(proxy, ck);
                          Console.WriteLine("forbidden " + e.Message);
                      }
                  }
              }
  </code></pre>
  
  <p>This application is running 24/7, and most of the time it's okay. But at certain times it just starts using 100% cpu. I believe this happens when the proxies are taking long to respond. Why could this possibly be the case, is there something in the code that I am overlooking that could cause the application to use 100% cpu?</p>