title: Multithreading a large number of web requests in c#
body: <p>I have an program where I need to create some large number of folders to an external sharepoint site (external meaning I can't use the sharepoint object model). Web requests work well for this, but simply doing them one at a time (send request, wait for response, repeat) is rather slow. I decided to multithread the requests, to try and speed it up. The program has sped up considerably, but after some amount of time (between 1-2 minutes or so), concurrency exceptions start getting thrown.</p>
  
  <p>Code is below, is this the best way to go about this?</p>
  
  <pre><code>Semaphore Lock = new Semaphore(10, 10);
  List&lt;string&gt; folderPathList = new List&lt;string&gt;();
  //folderPathList populated
  
  foreach (string folderPath in folderPathList)
  {
      Lock.WaitOne();
      new Thread(delegate()
      {
          WebRequest request = WebRequest.Create(folderPath);
          request.Credentials = DefaultCredentials;
          request.Method = "MKCOL";
  
          WebResponse response = request.GetResponse();
          response.Close();
          Lock.Release();
      }).Start();
  }
  for(int i = 1;i &lt;= 10;i++)
  {
      Lock.WaitOne();
  }
  </code></pre>
  
  <p>The exception is something along the lines of</p>
  
  <p>Unhandled Exception: System.Net.WebException: Unable  to connect to the remote server ---> System.Net.Sockets.SocketException: Only one useage of each socket address  is normally permitted 192.0.0.1:81<br>
  at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
  ss socketAddress)<br>
  at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)<br>
  at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket&amp; socket, IPAddress&amp; address, ConnectSocketState state,
  IAsyncResult asyncResult, Int32 timeout, Exception&amp; exception)</p>