title: Closing form from different thread happening too soon, causing exception
body: <p>I've got the following situation,</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Sub MyButton_Click(sender as Object, args as EventArgs) Handles MyButton.Click
      Me.pleaseWaitFrm = New PleaseWaitForm()
      ' Fire up new thread to do some work in (AddressOf DoMyWork)
      Me.pleaseWaitFrm.ShowDialog()
  End Sub
  
  Private Sub DoMyWork()
     Dim log = Me.DoTheActualWork()
  
     Me.pleaseWaitFrm.Close()
  
     Using logFrm as New LogViewer(log)
         logFrm.ShowDialog()
     End Using
  End Sub
  </code></pre>
  <p>If the <code>DoTheActualWork()</code> call exits fast enough, the <code>Me.pleaseWaitFrm.Close()</code> call is happening during the <code>Me.pleaseWaitFrm.ShowDialog()</code> call.  The result, no surprise, is this exception:</p>
  <blockquote>
  <p>An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll</p>
  <p>Additional information: Value Close() cannot be called while doing CreateHandle().</p>
  </blockquote>
  <p>Obviously this is the old &quot;you cannot .Close() a WinForm while it's in the process of loading&quot; problem.  But what isn't obvious to me is how best to prevent that from happening in this case?  How does one safely and reliably delay the .Close() until it is safe to do so in this situation?</p>