【发布时间】:2013-03-22 11:31:46
【问题描述】:
有很多主题涵盖了这个问题。不过我还是有问题。
我将程序集加载到新的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);
}
}
在加载程序集的 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(第一个try{} catch{})中捕获异常。
【问题讨论】:
标签: c# exception-handling appdomain