从内部卸载 AppDomain
我们已经构建了一个 WPF 可执行文件。
我想在子 AppDomain 中启动此可执行文件。
因此会有一个称为引导加载程序的主机AppDomain,它在单独的AppDomain中启动WPF应用程序。
WPF 应用程序可以触发自身更新,将新文件下载到临时文件夹中,然后需要向引导加载程序发出信号以卸载它并更新 WPF dll/文件。更新完成后,我们要重新启动 WPF 应用程序。
因此,首先我需要创建一个新域:
AppDomain wpfDomain = AppDomain.CreateDomain("WPF Domain");
wpfDomain.ExecuteAssembly(WPFAPP_PATH + "WpfApp.exe");
现在棘手的部分是弄清楚如何向引导加载程序发出信号以卸载 WPF 应用程序并更新它。
我尝试处理 DomainUnload
事件。它是在 WPF 域内调用的,因此我无法重新启动域。当尝试调用重新创建 WPF AppDomain 并执行程序集时,我收到 AppDomainUnloadedException
。
接下来,我读到卸载会中止域中的所有线程,因此我尝试为 WPF 域创建一个新线程并将主线程加入其中。下载更新时,我在内部调用了 AppDomain.Unload()
。
困难在于“父”(引导加载程序)AppDomain 不知道何时更新并重新启动子AppDomain,但我无法弄清楚子AppDomain 如何干净地通知父AppDomain,然后卸载。
想法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答您原来的问题:我能想到的一个解决方案是创建一个 ManualResetEvent ,您可以在退出时在子项中触发它。在加载器中,主线程正在等待此事件的信号。当您完成更新可执行文件并重新创建子域时,您可以再次重置事件。
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.