title: LoaderLock was detected in multithreaded C# application
body: <p>I am writing a WinForms application in C#.</p>
  
  <p>At one point in the application, I spawn a new STA thread (create thread, then <code>SetApartmentState</code>) that creates a new form and then shows it with plain old <code>Show()</code>. The form itself contains only a docked <code>DataGrid</code> whose <code>DataSource</code> points to a <code>DataTable</code> retrieved from a newly open <code>SqlConnection</code>. All data and UI objects are created on the same thread.</p>
  
  <p>When I execute that code I get a <code>LoaderLock</code> exception with the following (helpful) text:</p>
  
  <blockquote>
    <p>Attempting managed execution inside OS
    Loader lock. Do not attempt to run
    managed code inside a DllMain or image
    initialization function since doing so
    can cause the application to hang.</p>
  </blockquote>
  
  <p>I'm doing no such thing, at least not intentionally! The stack trace has only unmanaged code at the head and I'm unable to debug that with C# Express 2008.</p>
  
  <p>Looking around the Internets, people usually just disable that exception within VisualStudio. Is this another case of Microsoft being padantic, or should I actually spend time figuring out what's going on?</p>
  
  <p><strong>Update</strong> The error seemed to be caused by opening <strong>multiple</strong> forms (each with data grid, etc.) rapidly. If I switch the <code>Show()</code> to <code>ShowDialog()</code> and view the forms one at a time, then the error goes away.</p>