Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does BlockingCollection.Dispose actually do?

Tags:

.net

.net-4.0

What does BlockingCollection.Dispose actually do?

like image 239
Jonathan Allen Avatar asked Jul 06 '10 17:07

Jonathan Allen


2 Answers

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.

like image 28
Andy Robinson Avatar answered Oct 04 '22 03:10

Andy Robinson


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.

like image 87
Reed Copsey Avatar answered Oct 04 '22 02:10

Reed Copsey