public void Dispose() { // Only one thread can dispose StreamDisposeList _parent = Interlocked.CompareExchange(ref parent, null, parent); // Handle has already been disposed if (_parent == null) { return; } // Should dispose be started bool processDispose = false; // Decrement handle count lock (_parent.m_disposelist_lock) { int newCount = --_parent.belateHandleCount; // Is not the handle. if (newCount > 0) { return; } // Check Dispose() has been called when counter goes to 0 processDispose = Interlocked.Read(ref _parent.disposing) == 1L; } // Start dispose if (processDispose) { if (_parent.nonDisposable) { _parent.ProcessNonDispose(); } else { _parent.ProcessDispose(); } } }