title: Preventing Form closure from freezing Invoke calls
body: <p>In my application I've created a thread which draws on a <code>Bitmap</code> object, and when it is done drawing it calls <code>form.Invoke(form.Refresh)</code> which draws the bitmap to the main form. (<code>form</code> being a reference to the main form of my application.)</p>
  
  <p><strong>The Problem</strong>: I've noticed that if I close the form while the other thread is waiting for the callback message from UI thread (because of the <code>Invoke</code> call) - the thread will wait indefinitely(?) and will essentially get stuck, preventing the application from closing.</p>
  
  <p>Using <code>BeginInvoke</code> doesn't make sense in my program, because the time it takes to refresh the form has to be accounted for in the work of the thread.</p>
  
  <p><strong>Q:</strong> What would be the best way to prevent this from happening?</p>
  
  <p><strong>Edit:</strong> I've read in some places that its a bad idea, but it seems to me that the only way to deal with this is to call <code>Thread.Abort()</code>. I can't close the thread by using a flag or something similar, since its just stuck at the <code>Invoke</code> call.</p>
  
  <p><strong>Edit 2:</strong> To make it clear, what I'm looking for is a way to tell the worker thread that it should stop waiting for the <code>Invoke</code> callback and continue working.</p>