private void Dispose(bool disposing, bool takeLock = true) { bool lockTaken = false; if (takeLock) { Monitor.Enter(syncObject, ref lockTaken); } try { if (IsDisposed) { return; } IsDisposed = true; var exceptions = new List <Exception>(); foreach (var a in areas) { try { if (Win32FileMapping.UnmapViewOfFile(a.Address) == false) { throw new Win32Exception(); } } catch (Exception ex) { exceptions.Add(ex); } if (disposing) { try { a.Mmf.Dispose(); } catch (Exception ex) { exceptions.Add(ex); } } } if (disposing && exceptions.Count > 0) { throw new AggregateException(exceptions); } } finally { if (lockTaken) { Monitor.Exit(syncObject); } } if (disposing) { GC.SuppressFinalize(this); } }