title: C# multi-threading program with WebRequest
body: <p>First I am new in the forum so please have a little bit patience with me and my english. :-)</p>
  
  <p>I am writing a C# application which should send multithreaded SOAP requests to an apache backend. 
  Everything worked fine till now but I ran into a problem. The application first reads a XML file 
  from another system that is parsed first into classes, sorted and send to the SOAP backend. 
  Here the snippet</p>
  
  <pre><code>List&lt;Thread&gt; ThreadsPerOneRecord = new List&lt;Thread&gt;();         
  bool ExecuteSingleThreaded = false;
  //The variable list is passed as parameter to the function 
  
  foreach (Record prov in list)
  {
    XMLResult.AppendText("Type: " + prov.Type + Environment.NewLine);
  
    Thread t = new Thread(() =&gt; Send(prov, c));                                
    t.Start();
    //Here the sleep 
    Thread.Sleep(50);
    ThreadsPerOneRecord.Add(t);               
  
    #region Code for test single threaded execution
    if (ExecuteSingleThreaded)
    {
      foreach (Thread t2 in ThreadsPerOneRecord)
        t2.Join();
      ThreadsPerOneRecord.Clear();
    }
    #endregion
  }
  
  XMLResult.AppendText("Waiting for the threads to finish" + Environment.NewLine);
  //Waiting for the threads to finish
  foreach (Thread t in ThreadsPerOneRecord)            
    t.Join(); 
  </code></pre>
  
  <p>As I am sending this to the SOAP web service it works fine except one request. These requests are mixed up between each other. I.e.: </p>
  
  <pre><code>What it should be: 
  Record 1 -&gt; SOAP
  Record 2 -&gt; SOAP
  Record 3 -&gt; SOAP
  
  What it is
  Record 1 -&gt; SOAP
  Record 2 -&gt; SOAP 
  Record 2 -&gt; SOAP 
  Record 3 -&gt; nowhere
  </code></pre>
  
  <p>I already tried to debug the whole code and with the debugger it works fine. The same when I insert this sleep of 50 milliseconds. But without the sleep it mixes this two records  ...</p>
  
  <p>Does anybody have any idea why this happens? Shouldn't every thread be independent from itself? 
  Also I checked the collection and the data are correctly inside. </p>
  
  <p>Thanks</p>
  
  <p>Oldfighter</p>