如何防止后台线程中的异常终止应用程序? [英] How to prevent an exception in a background thread from terminating an application?

查看:202
本文介绍了如何防止后台线程中的异常终止应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以连接到 AppDomain.CurrentDomain.UnhandledException 以从后台线程记录异常,但是如何阻止它们终止运行时?

解决方案

首先,你应该尽量不要在后台线程中抛出 - 而不是处理异常。如果你控制着你的委托运行的方式,把它封装在一个try catch块中,并且把一个异常信息传回你的主线程(如果你明确地调用了BeginInvoke,或者通过在某处更新一些共享状态,使用EndInvoke) / p>

忽略未处理的异常可能是危险的。如果你有一个真正的不可处理的异常(OutOfMemoryException被考虑在内),你可以做任何事情都不会太多,你的进程基本上是注定失败的。



回到.Net 1.1在背景线程中的一个未处理的异常将被抛在无处,主线程很乐意犁。那可能会有令人讨厌的反响。所以在.Net 2.0中,这种行为已经改变了。



现在,一个不是主线程的线程抛出的未处理的异常将终止进程。您可能会收到通知(通过订阅AppDomain上的事件),但是该过程将会死亡。



由于这可能不方便(当您不知道什么将在线程中运行,你不是绝对确定它是正确的保护,你的主线程必须具有弹性),有一个解决方法。它的目的是作为遗留设置(意思是强烈建议您确保没有流行线程),但您可以通过以下方式强制以前的行为:



只需添加此设置对您的服务/应用程序/任何配置文件:

 < configuration> 
< runtime>
<! - 以下设置可防止主机在抛出未处理的异常时关闭 - >
< legacyUnhandledExceptionPolicy enabled =1/>
< / runtime>
< / configuration>

尽管如此,它似乎不适用于ASP.NET。



有关更多信息(以及在即将发布的CLR版本中可能不支持此设置的巨大警告)请参阅 http://msdn.microsoft.com/en-us/library/ms228965.aspx


I can hookup to AppDomain.CurrentDomain.UnhandledException to log exceptions from background threads, but how do I prevent them terminating the runtime?

解决方案

First, you really should try not to have exceptions thrown - and not handled - in a background thread. If you control the way your delegate is run, encapsulate it in a try catch block and figure a way to pass the exception information back to your main thread (using EndInvoke if you explicitly called BeginInvoke, or by updating some shared state somewhere).

Ignoring a unhandled exception can be dangerous. If you have a real un-handlable exception (OutOfMemoryException comes into mind), there's not much you can do anyway and your process is basically doomed.

Back to .Net 1.1, an unhandled exception in a backgroundthread would just be thrown to nowhere and the main thread would gladly plough on. And that could have nasty repercussions. So in .Net 2.0 this behavior has changed.

Now, an unhandled exception thrown in a thread which is not the main thread will terminate the process. You may be notified of this (by subscribing to the event on the AppDomain) but the process will die nonetheless.

Since this can be inconvenient (when you don't know what will be run in the thread and you are not absolutely sure it's properly guarded, and your main thread must be resilient), there's a workaround. It's intended as a legacy settings (meaning, it's strongly suggested you make sure you don't have stray threads) but you can force the former behavior this way :

Just add this setting to your service/application/whatever configuration file :

<configuration>
  <runtime>
    <!-- the following setting prevents the host from closing when an unhandled exception is thrown -->
    <legacyUnhandledExceptionPolicy enabled="1" />
  </runtime>
</configuration>

It doesn't seem to work with ASP.NET, though.

For more information (and a huge warning that this setting may not be supported in upcoming versions of the CLR) see http://msdn.microsoft.com/en-us/library/ms228965.aspx

这篇关于如何防止后台线程中的异常终止应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆