I want to launch a WPF App, in a 2nd AppDomain, from a 'loader' class. If the WPF App times itself out, I want it to fire an event back to the loader class and the loader class will Unload() the the 2nd AppDomain and show a login screen. If the user logs back in, the same process will repeat.
I have this working to a degree by :
Loader class creates the 2nd AppDomain and class B in that domain via CreateInstanceAndUnwrap.
Loader class creates a MarshalByRefObject that has a Timeout event, and passes it to a B.StartUp(MBRO), which passes the MBRO on to the constructor of the WPF App(). Loader class adds a handler to MBRO.Timeout.
WPF App times out, calls MBRO.Timeout, which is handled by the Loader class. In the event handler, Loader class shuts down B WPF App and shows the login window.
The problem is that I can't Unload the 2nd AppDomain in step 3. When I do it, it shuts down the host appdomain as well (no exceptions or anything, the whole thing shuts down).
I think the problem happens because the event handler delegate is being fired by the WPF App which is in the 2nd domain, and therefore I am trying to pull the rug from under App Domain from within a delegate it has fired.
Is this correct? Does it work like this across Domains?
In summary, can anyone suggest a way where you can launch a 2nd AppDomain, receive an event from the 2nd AppDomain and Unload() the 2nd AppDomain upon receiving that event? I think I need someone to decouple the receipt of the event from the act of Unloading the app domain.