什么是GC.SuppressFinalize的Dispose()方法的目的(这一点)? [英] What's the purpose of GC.SuppressFinalize(this) in Dispose() method?

查看:880
本文介绍了什么是GC.SuppressFinalize的Dispose()方法的目的(这一点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码看起来像这样的:

I have code that looks like this:

/// <summary>
/// Dispose of the instance
/// </summary>
public void Dispose()
{
    if (_instance != null)
    {
        _instance = null;
        // Call GC.SupressFinalize to take this object off the finalization
        //  queue and prevent finalization code for this object from
        //  executing a second time.
        GC.SuppressFinalize(this);
    }
}



虽然有评论认为解释说,GC的目的 - 相关电话,我仍然不明白为什么它的存在。

Although there is a comment that explains purpose of that GC-related call, I still don't understand why it's there.

不是Object运往垃圾收集一次,所有实例从存在终止(如,在使用时用()块)?

Isn't object destined for garbage collection once all instances cease from existence (like, when used in using() block)?

有什么用例场景,这将起到重要的作用?

What's the use case scenario where this would play important role?

谢谢!

推荐答案

在实现Dispose模式,你还可以添加一个终结你的类调用的Dispose()。这是为了确保的Dispose()总是的被调用,即使客户忘记调用它。

When implementing the dispose pattern you might also add a finalizer to your class that calls Dispose(). This is to make sure that Dispose() always gets called, even if a client forgets to call it.

要防止Dispose方法从两次运行(如果对象已被释放)您添加 GC.SuppressFinalize(本); 。该文件提供了一个样品

To prevent the dispose method from running twice (in case the object already has been disposed) you add GC.SuppressFinalize(this);. The documentation provides a sample:

class MyResource : IDisposable
{
    [...]

    // This destructor will run only if the Dispose method 
    // does not get called.
    ~MyResource()      
    {
        // Do not re-create Dispose clean-up code here.
        // Calling Dispose(false) is optimal in terms of
        // readability and maintainability.
        Dispose(false);
    }

    // Implement IDisposable.
    // Do not make this method virtual.
    // A derived class should not be able to override this method.
    public void Dispose()
    {
        Dispose(true);
        // This object will be cleaned up by the Dispose method.
        // Therefore, you should call GC.SupressFinalize to
        // take this object off the finalization queue 
        // and prevent finalization code for this object
        // from executing a second time.
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        // Check to see if Dispose has already been called.
        if(!this.disposed)
        {
            // If disposing equals true, dispose all managed 
            // and unmanaged resources.
            if(disposing)
            {
                // Dispose managed resources.
                component.Dispose();
            }

            // Call the appropriate methods to clean up 
            // unmanaged resources here.
            resource.Cleanup()          
        }
        disposed = true;         
    }
}

这篇关于什么是GC.SuppressFinalize的Dispose()方法的目的(这一点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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