質問をカバーするトピックがたくさんあります。しかし、それでも私には問題があります。
AppDomain
次のように、アセンブリを new にロードします。
public void Run()
{
//There's the problem.
//As Panos Rontogiannis mentioned the thread is created in default AppDomain
new Thread(RunApp).Start();
}
private void RunApp()
try
{
AppDomain.CreateDomain("domain name").ExecuteAssembly("path to assembly");
}
catch (Exception _e)
{
MessageBox.Show("Unhandled Exception.\n" + _e);
}
}
読み込まれたアセンブリの Main メソッドで、ハンドラーをUnhandledException
イベントにサブスクライブします。
AppDomain.CurrentDomain.UnhandledException += handleException;
ハンドラー自体:
public static void handleException(object a_s, UnhandledExceptionEventArgs a_args)
{
var _e = (Exception)a_args.ExceptionObject;
//Static loger class method
Loger.WriteError(_e.GetType().ToString(), _e.Message, "default solution");
}
ただし、読み込まれたアセンブリで例外がスローされても、ハンドラーは関与しません。AppDomain
デフォルト(first )でのみ例外をキャッチしますtry{} catch{}
。