防止第三方组件的异常导致崩溃整个应用程序 [英] Preventing Exceptions from 3rd party component from crashing the entire application

查看:914
本文介绍了防止第三方组件的异常导致崩溃整个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个依赖于第三方DLL的多线程应用程序。我的问题是,当使用第三方库中的对象时,如果它在运行时引发异常,我无法捕获它,它起泡并杀死整个应用程序。我有很多不同的线程,每个使用自己的对象从这个第三方库,我需要的线程,使用该对象的副本能够捕获和处理的异常。

I am writing a multi-threaded application that relies on some third party DLLs. My problem is that when using an object from the third party library, if it raises an exception while running, I am unable to catch it and it bubbles up and kills the entire application. I have many different threads each using its own object from this third party library and I need the thread that was using that copy of the object to be able to catch and deal with the exception.

基于我已经阅读的内容,似乎很可能是第三方库实际上正在做自己的线程和允许未捕获的异常。 .NET 2.0+行为允许这些异常终止整个应用程序。我知道AppDomain.CurrentDomain.UnhandledException,但是不允许你阻止应用程序关闭。

Based on what I've read, it seems like most likely the 3rd party library is actually making its own threads and allowing uncaught exceptions. The .NET 2.0+ behavior allows these exceptions to kill the entire application. I'm aware of AppDomain.CurrentDomain.UnhandledException, but that does not allow you to prevent application shutdown.

为了参考,我在.NET中编写一个控制台应用程序4.0。

For reference, I'm writing a console application in .NET 4.0. Does anyone have any solution/advice to stop these exceptions from killing my application?

推荐答案

有一件事你可以看看是< a href =https://msdn.microsoft.com/pl-pl/library/system.runtime.exceptionservices.handleprocesscorruptedstateexceptionsattribute%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 = nofollow> HandleProcessCorruptedStateExceptionsAttribute 属性。

One thing you might look at is the HandleProcessCorruptedStateExceptionsAttribute attribute.

我不知道这是不是你的问题,但是我不得不最近在调用第三方COM对象中的函数的方法上使用此属性。此属性是.net 4.0的新功能。我的基本的了解是,4.0框架将默认不鼓舞在某些情况下,它感觉第三方异常可能引入了一些不稳定的异常抛出。我认为这主要涉及第三方组件是非托管的情况。我不确定,但它解决了我的问题。

I don't know if this is your problem or not, but I had to recently use this attribute on a method that was calling a function in a third party COM object. This attribute is new to .net 4.0. My basic understanding is that the 4.0 framework will by default not bubble up an exception thrown in certain situations where it feels the 3rd party exception may have introduced some instabilty. I think this pertains mostly to situations where the 3rd party component is unmanaged. I am not sure, but it resolved my issue.

用法如下:

[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute()]
private void ThirdPartyCall()
{
    try
    {
            return Call3rdPartyFunction()
    }
    catch (Exception exInstantiate)
    {
        ...
    }
}

详情: http: //msdn.microsoft.com/en-us/magazine/dd419661.aspx

这篇关于防止第三方组件的异常导致崩溃整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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