The above post's answer (be me, also) is OK, but the drawback is that the forms closed during Application.Exit() are somewhat brutally killed. In my case, that was fine.
A simple but more polite way to do it is as follows. Assume that one form has spawned another, but the second is on a separate thread. When the "parent" or original thread starts to close, it can close the other form, but it must do so correctly. It can't just call
frmOther.Close(), because almost all form members require they be called on the form's main thread.
private FormTestThreading frmtt = null;
public delegate void SimpleCall ();
private void FormXlate_FormClosing ( object sender, FormClosingEventArgs e )
{
if ( frmtt != null )
{
SimpleCall sc = new SimpleCall( frmtt.Close );
frmtt.BeginInvoke( sc ); frmtt = null;
}
}
The code above creates a delegate and performs a safe, cross-thread call to the other form's Close() method. Ignoring the returned IAsyncWhatever appears to be safe according to the documentation I've read.
Generally, this will cause the form-running separate thread to exit its message loop and return back to the thread's start function. That exits, causing the thread itself to die.