title: C# Threading/Lock confusion
body: <p>I have the following code:</p>
  
  <pre><code>var items = new List&lt;string&gt; {"1", "2", "3"}; // 200 items
  foreach(var item in items) {
    ThreadPool.QueueUserWorkItem((DoWork), item);
  }
  
  private void DoWork(object obj)
  {
    lock(this)
    {
      using(var sw = File.AppendText(@"C:\somepath.txt")
      {
        sw.WriteLine(obj);
      }
    }
  }
  </code></pre>
  
  <p>Because of the threading, for some reason, I get a random number of the 200 items written out to the file.  60 or 127 or sometimes only 3.  If I remove the ThreadPool and just write inside the original foreach loop, all 200 items are written successfully.</p>
  
  <p>Not sure why this occurs?</p>
  
  <p>Thanks for your help.</p>