using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Wood.Util { public class ConcurrentList : IList { protected static object _lock = new object(); protected List _interalList = new List(); public IEnumerator GetEnumerator() { return Clone().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return Clone().GetEnumerator(); } public int Count { get { return _interalList.Count; } } public bool IsReadOnly { get { return false; } } public T this[int index] { get { lock (_lock) { return _interalList[index]; } } set { lock (_lock) { _interalList[index] = value; } } } public List Clone() { List newList = new List(); lock (_lock) { _interalList.ForEach(x => newList.Add(x)); } return newList; } public int IndexOf(T item) { return _interalList.IndexOf(item); } public void Insert(int index, T item) { _interalList.Insert(index, item); } public void RemoveAt(int index) { lock (_lock) { _interalList.RemoveAt(index); } } public void Add(T item) { lock (_lock) { _interalList.Add(item); } } public void AddRange(IEnumerable list) { foreach (T item in list) { Add(item); } } public void Clear() { lock (_lock) { _interalList.Clear(); } } public bool Contains(T item) { return _interalList.Contains(item); } public void CopyTo(T[] array, int arrayIndex) { _interalList.CopyTo(array, arrayIndex); } public bool Remove(T item) { if (item == null) { return false; } lock (_lock) { return _interalList.Remove(item); } } public void RemoveAll(Predicate match) { if (match == null) { return; } Contract.Ensures(Contract.Result() >= 0); Contract.Ensures(Contract.Result() <= Contract.OldValue(Count)); Contract.EndContractBlock(); foreach (T t in Clone()) { if (match(t)) { Remove(t); } } } } }