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

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

问题描述

我联播到 AppDomain.CurrentDomain.UnhandledException 来记录从后台线程的例外,但我怎么prevent他们终止运行系统?

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

推荐答案

首先,你真的应该尽量不要有异常抛出 - 而不是处理 - 在后台线程。如果你能够控制自己委托的运行方式,封装在一个try catch块和数字的方式来传递异常信息返回到你的主线程(使用EndInvoke会,如果你明确要求的BeginInvoke,或在某处更新一些共享的状态)。

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).

忽略一个未处理的异常可能是危险的。如果你有一个真正的联合国handlable异常(OutOfMemoryException异常进入脑海),有没有什么可以做不管怎样,你的进程基本上是注定要失败。

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.

返回到.NET 1.1,在backgroundthread未处理的异常也只是抛出无处和主线程会乐意犁的。这可能有讨厌的反响。因此,在NET 2.0此行为已更改。

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.

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

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>

这似乎不使用ASP.NET的工作,虽然。

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

有关详细信息(和一个巨大的警告,这种设置可能无法在即将到来的CLR版本的支持)请参阅<一href="http://msdn.microsoft.com/en-us/library/ms228965.aspx">http://msdn.microsoft.com/en-us/library/ms228965.aspx

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

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

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