简体   繁体   English

BlockingCollection.Dispose实际上做了什么?

[英]What does BlockingCollection.Dispose actually do?

BlockingCollection.Dispose实际上做了什么?

This allows the internal wait handles to be disposed of properly. 这允许正确处理内部等待句柄。

BlockingCollection<T> , internally, uses a pair of event wait handles, which in turn have an associated native HANDLE . BlockingCollection<T>内部使用一对事件等待句柄,而这些句柄又具有关联的本机HANDLE

Specifically, BlockingCollection<T>.Dispose() releases these two handles back to the operating system, by eventually (through SemaphoreSlim->ManualResetEvent) calling the native CloseHandle method on the two native HANDLE instances. 具体来说, BlockingCollection<T>.Dispose()将这两个句柄释放回操作系统,最终(通过SemaphoreSlim-> ManualResetEvent)在两个本机HANDLE实例上调用本机CloseHandle方法。

Having a quick look with reflector reveals this... 快速浏览一下反射器就会发现......

protected virtual void Dispose(bool disposing)
{
    if (!this.m_isDisposed)
    {
        if (this.m_freeNodes != null)
        {
            this.m_freeNodes.Dispose();
        }
        this.m_occupiedNodes.Dispose();
        this.m_isDisposed = true;
    }
}

and m_freeNodes is private SemaphoreSlim m_freeNodes; m_freeNodesprivate SemaphoreSlim m_freeNodes; so it releases the SemaphoreSlim that are used internally. 所以它释放内部使用的SemaphoreSlim。

Releases all resources used by the current instance of the BlockingCollection<T> class. 释放BlockingCollection<T>类的当前实例使用的所有资源。 ( Source ) 来源

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

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