title: "Invoke or BeginInvoke cannot be called..." when closing a window
body: <p>Ok there are lots of questions on this error, but I can't seem to find a solution for my issue. I have a thread that loads a control from the database during a long running operation. If I close my window before that thread finishes, it throws a  <code>Invoke or BeginInvoke cannot be called on a control until the window handle has been created.</code>, when doing the <code>Invoke</code></p>
  
  <p>I've modified the code to look like the following:</p>
  
  <pre><code>If Not Me.IsDisposed And Me.Created And Me.IsHandleCreated Then '
        Me.Invoke(New AddImageDelegate(AddressOf AddImage), item.PartNo, New Bitmap(item.ImagePath))
        Me.Invoke(New SetImageDelegate(AddressOf SetImage), I, item.PartNo)
  End If
  </code></pre>
  
  <p>However it still hits the <code>Invoke</code> line and throws the error. When debugging stops on the exception, <code>IsDisposed</code> is true, <code>.Created</code> is false, and <code>.IsHandleCreated</code> is false. So checking those properties doesn't help.</p>
  
  <p>So... how do i prevent it from running the invoke line in this situation?</p>