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

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

问题描述

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



根据我所看到的,似乎很可能第三方库实际上是自己创建的线程,并允许未被捕获的异常。 .NET 2.0+行为允许这些异常终止整个应用程序。我知道AppDomain.CurrentDomain.UnhandledException,但是这不允许你阻止应用程序关闭。



为了参考,我在.NET中编写一个控制台应用程序4.0。有没有人有任何解决方案/建议来阻止这些例外从杀死我的应用程序?

解决方案

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



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



使用情况如下所示:

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

更多信息: http ://msdn.microsoft.com/en-us/magazine/dd419661.aspx


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.

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.

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?

解决方案

One thing you might look at is the HandleProcessCorruptedStateExceptionsAttribute attribute.

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.

The usage looks like this:

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

More information: http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

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

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