# HG changeset patch # User cin # Date 1493139093 -10800 # Node ID 1e082fb67a46915db43a699c17fd3b70b41b0f49 # Parent fe51010831503b05bd4086536e549d63b1577995 Fixed component container diff -r fe5101083150 -r 1e082fb67a46 Implab/Components/ComponentContainer.cs --- a/Implab/Components/ComponentContainer.cs Fri Apr 21 14:27:17 2017 +0300 +++ b/Implab/Components/ComponentContainer.cs Tue Apr 25 19:51:33 2017 +0300 @@ -8,7 +8,8 @@ /// /// Instanses of this class are thread safe. public class ComponentContainer : Disposable, ICollection { - readonly HashSet m_components = new HashSet(); + List m_components = new List(); + readonly object m_lock = new object(); /// /// Removes currently stored compoenents from the container and disposes them if possible. @@ -17,12 +18,11 @@ /// A new components may be added before this method completes. /// public void Clear() { - T[] removed; + List removed; - lock (m_components) { - removed = new T[m_components.Count]; - m_components.CopyTo(removed); - m_components.Clear(); + lock (m_lock) { + removed = m_components; + m_components = new List(); } foreach (var item in removed.OfType()) @@ -34,7 +34,7 @@ /// /// The item to check. public bool Contains(T item) { - lock (m_components) + lock (m_lock) return m_components.Contains(item); } @@ -44,7 +44,7 @@ /// A destination array for components. /// A starting index in the destination array. public void CopyTo(T[] array, int arrayIndex) { - lock (m_components) + lock (m_lock) m_components.CopyTo(array, arrayIndex); } @@ -53,7 +53,7 @@ /// /// The item to remove. public bool Remove(T item) { - lock (m_components) + lock (m_lock) return m_components.Remove(item); } @@ -62,7 +62,7 @@ /// public int Count { get { - lock (m_components) + lock (m_lock) return m_components.Count; } } @@ -84,12 +84,11 @@ /// /// The enumerator. public IEnumerator GetEnumerator() { - T[] items; - lock (m_components) { - items = new T[m_components.Count]; + T[] items = new T[m_components.Count]; + lock (m_lock) { m_components.CopyTo(items); - return (IEnumerator)items.GetEnumerator(); } + return (IEnumerator)items.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { @@ -105,13 +104,15 @@ /// public void Add(T item) { Safe.ArgumentNotNull(item, "item"); - - lock (m_components) { + bool dispose = false; + lock (m_lock) { if (IsDisposed) - Safe.Dispose(item); + dispose = true; else m_components.Add(item); } + if (dispose) + Safe.Dispose(item); } /// @@ -119,8 +120,10 @@ /// /// If set to true the collection is disposing. protected override void Dispose(bool disposing) { + if (disposing) + Clear(); + base.Dispose(disposing); - Clear(); } } }