我应该如何优雅地处理有问题的 AppDomain?
这段代码设计得不好吗?最初,finally 块中只有一个 AppDomain.Unload
。这带来了不幸的副作用,即当 UnhandledException
运行时,其他线程可能会在 AppDomain 中继续运行,其中使用用户输入,因此在计算规模上非常慢(平均实际运行时间可能是 > ;1 分钟),可能会引发其他异常,并且通常会导致更多问题。我一直在思考一种“更好”的方法来做到这一点,所以,我将其提交给SO。把你的想法借给我吧。
注意:我刚刚意识到这里也存在同步问题。是的,我知道它们是什么,让我们保持专注。
mainApp = AppDomain.CreateDomain(ChildAppDomain, null, AppDomain.CurrentDomain.SetupInformation);
try
{
mainApp.ExecuteAssembly(Assembly.GetEntryAssembly().Location);
finished = true;
}
catch (Exception ex)
{
AppDomain.Unload(mainApp);
mainApp = null;
UnhandledException(this, new UnhandledExceptionEventArgs(ex, false));
}
finally
{
if (mainApp != null)
{
AppDomain.Unload(mainApp);
mainApp = null;
}
}
// ...
void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (mainApp != null)
{
AppDomain.Unload(mainApp);
mainApp = null;
}
// [snip]
}
Is this code snippet poorly designed? Originally, there was only one AppDomain.Unload
, in the finally block. This had the unfortuanate side effect that other threads could keep running in the AppDomain while UnhandledException
was running, which among other things uses user input and is hence very slow on a computing scale (average real runtime might be >1min), potentially throwing other exceptions and generally causing more problems. I'm stuck on thinking of a 'better' way of doing this, so, I submit this to SO. Lend me your minds.
Note: I just realised there's synchronisation issues here too. Yes, I know what they are, lets stay focused.
mainApp = AppDomain.CreateDomain(ChildAppDomain, null, AppDomain.CurrentDomain.SetupInformation);
try
{
mainApp.ExecuteAssembly(Assembly.GetEntryAssembly().Location);
finished = true;
}
catch (Exception ex)
{
AppDomain.Unload(mainApp);
mainApp = null;
UnhandledException(this, new UnhandledExceptionEventArgs(ex, false));
}
finally
{
if (mainApp != null)
{
AppDomain.Unload(mainApp);
mainApp = null;
}
}
// ...
void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (mainApp != null)
{
AppDomain.Unload(mainApp);
mainApp = null;
}
// [snip]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会努力不重复。在我看来,您只需清理finally块中的appdomain就可以完成这项工作,就像您最初所做的那样。这个想法是,如果发生未处理的异常,请将其放入变量中并在关闭应用程序域后对其进行处理。
I would strive for no duplication. And imo you could make this work with only cleaning up the appdomain in your finally block, as you did initially. The idea being that if an unhandled exception occurs, place it in a variable and process it after shutting down the appdomain.