开发者

What does BlockingCollection.Dispose actually do?

开发者 https://www.devze.com 2023-01-06 16:35 出处:网络
What does BlockingCollection.Disp开发者_如何学Cose actually do?This allows the internal wait handles to be disposed of properly.

What does BlockingCollection.Disp开发者_如何学Cose actually do?


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.

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.


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; so it releases the SemaphoreSlim that are used internally.


Releases all resources used by the current instance of the BlockingCollection<T> class. (Source)

0

精彩评论

暂无评论...
验证码 换一张
取 消