简体   繁体   English

C#BlockingCollection Dispose方法

[英]C# BlockingCollection Dispose method

Documentation of BlockingCollection<T> class has the following note: BlockingCollection<T>类的文档具有以下注意事项:

Always call Dispose before you release your last reference to the 
BlockingCollection<T>. Otherwise, the resources it is using will not be freed 
until the garbage collector calls the BlockingCollection<T> object's Finalize 
method.

And internal implementation of BlockingCollection<T> in C# has the following method: C#中的BlockingCollection<T> 内部实现具有以下方法:

/// <summary>
/// Releases resources used by the <see cref="T:System.Collections.Concurrent.BlockingCollection{T}"/> instance.
/// </summary>
/// <param name="disposing">Whether being disposed explicitly (true) or due to a finalizer (false).</param>
protected virtual void Dispose(bool disposing)

There is only one call of this Dispose with argument disposing: true and finalization suppress after the call. 仅使用此参数Dispose调用此Dispose disposing: true调用之后为disposing: true和finalize。 But, surprisingly for me, there is no explicit finalizer in the class, and even no call of Dispose(false) . 但是,令我惊讶的是,该类中没有显式的终结器,甚至没有Dispose(false)调用。 It looks like Dispose function is relatively simple and it just removes references to different objects for GC speed up. 看起来Dispose函数相对简单,它只是删除了对不同对象的引用以提高GC的速度。 In this case, GC will do this work for us in the case when we forget to call Dispose() explicitly. 在这种情况下,当我们忘记显式调用Dispose()时,GC将为我们完成这项工作。

Can someone spot the light on the internals of this class? 有人可以看到这个班级的内部吗? What is the purpose of a method Dispose(bool disposing) ? 方法Dispose(bool disposing)的目的是什么? Is it common practice to implement this method for .NET core libraries even in cases when it is not needed? 是否在不需要.NET核心库的情况下实现此方法,这是惯例吗?

I think the MSDN documentation for how to properly implement the dispose pattern is worth a read. 我认为有关如何正确实现处置模式的MSDN文档值得一读。 However, the answer to your question is that BlockingCollection is not sealed. 但是,您的问题的答案是BlockingCollection没有密封。 This means that it can be derived. 这意味着可以派生它。 The reason for the void Dispose(bool disposing) is to allow derived classes to properly de-allocate the resources of the base class. void Dispose(bool disposing)是允许派生类适当地重新分配基类的资源。 So, for example, I could implement 因此,例如,我可以实施

class Foo : BlockingCollection<string>
{
  private bool disposed = false;

  ~Foo()
  {
    Dispose(false);
  }

  protected override void Dispose(bool disposing)
  {
    if (disposed)
    {
      return;
    }

    if (disposing)
    {
      // free managed resources
    }

    // free unmanaged resources

    disposed = true;
    base.Dispose(disposing);
  }
}

By doing this, when Dispose() is called, the BlockingCollection will call Dispose(true) on Foo, which will eventually call Dispose(true) on BlockingCollection, and you get the benefit that the ~Foo() finalizer is suppressed. 这样,在调用Dispose()时,BlockingCollection将在Foo上调用Dispose(true) ,最终将在BlockingCollection上调用Dispose(true) ,您将获得〜Foo()终结器被抑制的好处。 If the Dispose() method is not called, the finalizer is not suppressed, and it is called, allowing Foo to still deallocate its unmanaged resources. 如果未调用Dispose()方法,则不会抑制终结器,而是调用该终结器,从而允许Foo仍然释放其非托管资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM