有很多关于这个问题的主题。但是我还是有一个问题。
我像这样将程序集加载到新的AppDomain
中:
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);
}
}
在加载程序集的主方法中,我将处理程序订阅到
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
(第一个try{} catch{}
)中捕获异常。
ExecuteAssembly
在默认AppDomain
中创建的新Thread
中被调用。但是,在新的AppDomain
中加载的程序集内部处理异常非常重要。 - wazelin