diff Implab/Components/ComponentContainer.cs @ 216:1e082fb67a46 v2

Fixed component container
author cin
date Tue, 25 Apr 2017 19:51:33 +0300
parents b933ec88446e
children
line wrap: on
line diff
--- 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 @@
     /// </summary>
     /// <remarks>Instanses of this class are thread safe.</remarks>
     public class ComponentContainer<T> : Disposable, ICollection<T> {
-        readonly HashSet<T> m_components = new HashSet<T>();
+        List<T> m_components = new List<T>();
+        readonly object m_lock = new object();
 
         /// <summary>
         /// 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.
         /// </remarks>
         public void Clear() {
-            T[] removed;
+            List<T> 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<T>();
             }
 
             foreach (var item in removed.OfType<IDisposable>())
@@ -34,7 +34,7 @@
         /// </summary>
         /// <param name="item">The item to check.</param>
         public bool Contains(T item) {
-            lock (m_components)
+            lock (m_lock)
                 return m_components.Contains(item);
         }
 
@@ -44,7 +44,7 @@
         /// <param name="array">A destination array for components.</param>
         /// <param name="arrayIndex">A starting index in the destination array.</param>
         public void CopyTo(T[] array, int arrayIndex) {
-            lock (m_components)
+            lock (m_lock)
                 m_components.CopyTo(array, arrayIndex);
         }
 
@@ -53,7 +53,7 @@
         /// </summary>
         /// <param name="item">The item to remove.</param>
         public bool Remove(T item) {
-            lock (m_components)
+            lock (m_lock)
                 return m_components.Remove(item);
         }
 
@@ -62,7 +62,7 @@
         /// </summary>
         public int Count {
             get {
-                lock (m_components)
+                lock (m_lock)
                     return m_components.Count;
             }
         }
@@ -84,12 +84,11 @@
         /// </summary>
         /// <returns>The enumerator.</returns>
         public IEnumerator<T> 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<T>)items.GetEnumerator();
             }
+            return (IEnumerator<T>)items.GetEnumerator();
         }
 
         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
@@ -105,13 +104,15 @@
         /// </remarks>
         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);
         }
 
         /// <summary>
@@ -119,8 +120,10 @@
         /// </summary>
         /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param>
         protected override void Dispose(bool disposing) {
+            if (disposing)
+                Clear();
+
             base.Dispose(disposing);
-            Clear();
         }
     }
 }