终结未处理的异常,甚至有CriticalFinalizerObject以后不叫 [英] Finalizer not called after unhandled exception even with CriticalFinalizerObject

查看:261
本文介绍了终结未处理的异常,甚至有CriticalFinalizerObject以后不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有测试code是这样的:

I have test code like this:

public class A : CriticalFinalizerObject 
{
    ~A()
    {
        File.WriteAllText("c:\\1.txt", "1z1z1");
    }
}

class Program
{
    static void Main(string[] args)
    {
        A a = new A();
        throw new Exception();
    }
}

首先,我试图运行而无需从CriticalFinalizerObject导出。终结该程序结束后,不叫。 这让我吃惊,因为我认为这是更具有确定性,但没关系。然后,我读过有关CriticalFinalizerObject的,以确保他们的终结将被调用。我从它衍生出的。 你猜怎么了。它仍然没有得到执行。 我在做什么/理解错了?

First I tried running it without deriving A from CriticalFinalizerObject. Finalizer wasn't called after end of this program. That surprised me as I thought it was more deterministic but okay. Then I've read about CriticalFinalizerObject's that ensure their finalizers will be called. I derived A from it. Guess what. It still doesn't get executed. What am I doing/understanding wrong?

(请不要写关于垃圾收集器不确定性显而易见的东西,我知道,这是不是这样的节目结束了,我想像我以后一个不错的未处理的管理可以安全地清理的例外。)

(Please don't write obvious stuff about garbage collector being non-deterministic, I know that. It is not the case as the program is over and I imagined I could safely clean up after a nice unhandled managed exception.)

推荐答案

首先,让我们来了解 CriticalFinalizerObject 的<一个href="http://msdn.microsoft.com/ru-ru/library/system.runtime.constrainedexecution.criticalfinalizerobject.aspx">MSDN,我们可以看到,认为:

Firstly, let's read about CriticalFinalizerObject in MSDN, we can read, that:

在自CriticalFinalizerObject类的派生类,公共语言运行库(CLR)保证所有关键终止code将得到执行的机会,提供的终结遵循规则的CER,甚至在情况下的CLR强行卸载应用程序域或终止线程。

In classes derived from the CriticalFinalizerObject class, the common language runtime (CLR) guarantees that all critical finalization code will be given the opportunity to execute, provided the finalizer follows the rules for a CER, even in situations where the CLR forcibly unloads an application domain or aborts a thread.

这里主要的字是取出

其次,让我们看 MSDN ,这一次关于管理线程异常:

Secondly, let's read MSDN again, this time about Exceptions in managed threads:

如果这些异常是未处理的主线程,或者在进入从非托管code运行时的线程,他们正常进行,造成的终止的应用程序。

If these exceptions are unhandled in the main thread, or in threads that entered the runtime from unmanaged code, they proceed normally, resulting in termination of the application.

主词是终止

所以,当有未处理的异常在主线程 - 应用程序终止,但CriticalFinalizerObject有助于仅在域的卸载

So, when there is an unhandled exception in main thread - app terminates, but CriticalFinalizerObject helps only on unloading of Domain.

例如,Crit​​icalFinalizerObject可以帮助在这样的情况:

For example, CriticalFinalizerObject can helps in such situation:

// Create an Application Domain:
AppDomain newDomain = AppDomain.CreateDomain("NewApplicationDomain");

// Load and execute an assembly:
newDomain.ExecuteAssembly(@"YouNetApp.exe");

//Unload of loaded domain
AppDomain.Unload(newDomain);

这是一种情况,其中域被卸载,并CriticalFinalizerObject向你保证,你的终结将被调用。

This is a situation, where domain was unloaded, and CriticalFinalizerObject guarantee you, that your finalizer will be called.

在与应用程序的终止你的情况,你可以尝试订阅

In your situation with terminating of app you can try to subscribe to

AppDomain.CurrentDomain.UnhandledException

和手动完成你的对象。

UPD: 杰弗里里希特在他的著作CLR通过C#写CriticalFinalizerObject,它的对,你把你的code例如以SQLServer的情况下,它可以运行C#作为一个程序。在这种情况下CriticalFinalizerObject可以帮助你清理你的对象,如果SQLServer的将卸载磁带库的域名。 此外CriticalFinalizerObject是,你在对象的终结需要调用另一个对象的方法的情况下,因为CriticalFinalizerObject的向你保证,那它的终结将所有非CriticalFinalizerObject对象终结后调用。

UPD: Jeffrey Richter in his book "CLR via C#" wrote about CriticalFinalizerObject, that it's for situations where you send your code for example to SQLServer, which can run C# as a procedures. In such case CriticalFinalizerObject helps you to clean your object, if SQLServer will unload your library's Domain. Also CriticalFinalizerObject is for situations where you need in finalizer of object to call method of another object, because of CriticalFinalizerObject guarantee you, that it's finalizer will be called after finalizers of all non CriticalFinalizerObject objects.

这篇关于终结未处理的异常,甚至有CriticalFinalizerObject以后不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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