title: C# Limit http requests per second
body: <p>i have tool that is using proxies to send http requests.</p>
  
  <p>I need help with limiting http requests per second.</p>
  
  <p>Currently i'm using following code, but i know this is wrong method.</p>
  
  <p>So my question is how i can limit maximum 300 connections per second and queue threads that executed method while limit?</p>
  
  <p>Using xNet:</p>
  
  <pre><code>        static private string generateToken(ProxyType proxyType, string proxy)
      {
          string tokenResponse = null;
          try
          {
              WaitForConnection();
  
              using (var request = new HttpRequest())
              {
                  request.AllowAutoRedirect = true;
                  request.ConnectTimeout = 5000;
                  request.Reconnect = false;
                  request.ReconnectDelay = 5000;
                  request.ReconnectLimit = 1;
                  request.Proxy = Socks5ProxyClient.Parse(proxy);
  
  
                  HttpResponse httpWebRequestresponse = request.Get("http://example.com");
  
                  tokenResponse = httpWebRequestresponse.ToString();
              }
  
          }
          finally
          {
              totalConnections--;
          }
          return tokenResponse;
      }
  
  static void WaitForConnection()
      {
          bool canConnect = false;
          do
          {
              if (totalConnections &lt;= maxconnectionsNum)
              {
                  canConnect = true;
              }
          } while (canConnect == false);
          totalConnections++;
      }
  </code></pre>