(.net) CriticalFinalizerObject - What does it really do?

一笑奈何 提交于 2019-11-30 20:56:02

from a couple of tests I did, it doesn't seem to be true.

Finalizers in .Net are non-deterministic. That means there's no guarantee exactly when the finalizer will be called. Just because an object went out of scope or even was disposed, doesn't mean the finalizer will be called right away. The garbage collector will get around to it at some unknown time in the future.

If you really need code to run when when your program is Ctrl+Alt+Del'd, I don't think there's any other way than to have a separate program that monitors the first's state. If you really need that much architecture, I think you'd want to be using a service and some client apps, or a pair or services.

This is assuming, though, that you've already looked into the Application events. If you haven't, check out this overview.

EDIT Better than that overview, probably, is the ApplicationExit event.

The Finalize() method and Dispose() method are different things.

By default, Dispose would never be called. You would have to call it yourself from the Finalize method. Consider the following (ignoring the obvious failures in a proper finalize/dispose pattern here, for brevity):

public class Foo : IDisposable
{
    public void Dispose() 
    {
        // NOP
    }

    ~Foo()
    {
        Dispose();
    } 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!