views:

94

answers:

0

My winforms application does something like the following:

* Please note, the code below works as expected and blocks on singleInstance.Run(new string[0]{});

I haven't been able to reproduce the problem outside of my application

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var form = new Form1();
        Application.Run(form);
        var singleInstance = new SingleInstance();
        singleInstance.Run(new string[0]{}); //should block here right?
    }

    public class SingleInstance : WindowsFormsApplicationBase
    {
        protected override void OnCreateMainForm()
        {
            MainForm = new Form2();
        }
    }
}

My app however does this:
1. starts up one form, blocks till it's closed
2. Then starts the main app
3. Then closes everything, the main forms flickers on screen and then it's dispose method is called. It does not block on the call to singleInstance.Run()

Other things I have tried are showing form1 on another thread and blocking the main thread till its finished. This sort of works until backgroundworker threads are used in the main app. The WorkCompleted events however come back on the same thread as the DoWork methods causing untold cross-thread exceptions. I have mitigated this effect by calling

WindowsFormsSynchronizationContext.Uninstall();

on the main thread after the 1st thread blocks, but now my top level Application.ThreadException event handler doesn't fire!!

Can someone point me at some docs or pass on their wisdom about the relationship between the static Application class, and how it manages thread affinity with the message pump as my current hunch is this is what has been broken somehow...

I haven't been able to replicate the problem outside my app unfortunately either...