バックグラウンドスレッドでAPI「ExecuteAssembly」を使用して、セカンダリAppDomainでWPFアプリケーションを起動しています。このセカンダリアプリケーションを閉じると(たとえば、ユーザーがメインウィンドウを閉じると)、アプリケーションがスタンドアロンのAppDomain内で実行された場合のように、通常のシャットダウンシーケンスが実行されないことがわかりました。具体的には、ApplicationクラスのOnExitメソッドは呼び出されません。
他の誰かがこれに遭遇しましたか?
たとえば、セカンダリアプリドメインを開始します
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();