title: Collection was modified exception on datatable.Copy()
body: <p>I am getting the following exception when I call <code>datatable.Copy()</code> and I'm not sure why. I applied a lock to avoid this issue but it didn't seem to fix it. The exception doesn't happen consistently so I know it has to do with threading. Am I misunderstanding how locks work?</p>
  
  <blockquote>
    <p>System.InvalidOperationException: Collection was modified; enumeration operation might not execute.
      at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
      at System.Data.DataTable.Copy()</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>DataTable dt;
  
  lock (ClassName.datatable)
  {
      dt = ClassName.datatable.Copy();
  }
  </code></pre>
  
  <p><code>ClassName</code> is a type and not an object (there was some confusion in a previous question of mine).</p>