We have built a WPF executable.
I want to launch this executable within a child AppDomain.
So there will be a host AppDomain called the boot loader, which launches the WPF app in a separate AppDomain.
The WPF application can trigger an update for itself that downloads new files into a temporary folder, and then needs to signal the boot loader to unload it and update the WPF dlls / files. Once the update is complete, we want to restart the WPF app.
So first I need to create a new domain:
AppDomain wpfDomain = AppDomain.CreateDomain("WPF Domain");
wpfDomain.ExecuteAssembly(WPFAPP_PATH + "WpfApp.exe");
Now the tricky part is figuring out how to signal the boot loader to unload the WPF app and update it.
I tried handling the DomainUnload event. It’s called within the WPF Domain, so I can’t restart the domain. I get an AppDomainUnloadedException when trying to call recreate the WPF AppDomain and execute the assembly.
Next I read that unload aborts all of the threads in the domain, so I tried creating a new thread for WPF Domain and joining the main thread to it. I called AppDomain.Unload() internally when the updates were downloaded.
The difficulty is that the “parent” (boot loader) AppDomain doesn’t know when to update and restart the child AppDomain, but I can’t figure out how the child can cleanly notify the parent and then get unloaded.
Thoughts?
To answer your original question: One solution I could think of is creating a ManualResetEvent which you can trigger in the child when exiting. In the loader you have the main thread waiting for a signal on this event. When you finish updating the executables and are recreating the child domain, you can reset the event again.