private void exitMenuItem Click(object sender, EventArgs e)
{
    if (runtime != null && runtime.Running)
    {
        ThreadPool.QueueUserWorkItem(delegate(object obj)
        {

            runtime.Stop();
            Invoke(new ThreadStart(delegate()
            {

                this.Close();
            }));
        });
    }
    else
    {
        this.Close();
    }
           
}

Just caught myself coding this up. The shown method is a Windows Forms event handler for a menu item. The task at hand is to check a local variable and if that’s set to a specific value, to switch to a different thread and perform an action (that particular job can’t be done on the Windows Forms STA thread), and to close the form back on the main STA thread once that’s done. I colored the executing threads; yellow is the Windows Forms STA thread, blue is an arbitrary pool thread. Works brilliantly. Sick, eh?

 

[Mind that I am using ThreadStart just because it’s a convenient void target(void) delegate]

Updated: