我在后台线程中使用 API“ExecuteAssembly”在辅助 AppDomain 中启动 WPF 应用程序。当这个辅助应用程序关闭时(例如用户关闭主窗口),我发现正常的关闭序列不会像应用程序在独立的 AppDomain 中运行时那样被执行。具体来说,Application类的OnExit方法没有被调用。
有人遇到过这种情况么?
例如启动我的辅助 appdomain
var thread = new Thread(() =>
{
var currentAppDomain = Thread.GetDomain();
try
{
// line below blocks until complete
currentAppDomain.ExecuteAssembly("PathToAssemblyToExecute");
// at this point, executing assembly has been closed
}
catch (ThreadAbortException)
{
// do nothing. this can occur due to timing issues. framework seems
// to send this exception out when unloading an appdomain
}
catch (Exception)
{
// an unhandled exception has occured in this app
AppDomain.Unload(currentAppDomain);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();