Created
February 17, 2011 06:12
-
-
Save kthompson/831122 to your computer and use it in GitHub Desktop.
SyncCollection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Linq; | |
using System.Text; | |
namespace Gitty | |
{ | |
class SyncCollection<T> : ICollection<T> | |
{ | |
private object _syncRoot = new object(); | |
private Collection<T> _collection = new Collection<T>(); | |
public IEnumerator<T> GetEnumerator() | |
{ | |
lock (_syncRoot) | |
{ | |
foreach (var item in _collection) | |
{ | |
yield return item; | |
} | |
} | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public void Add(T item) | |
{ | |
lock (_syncRoot) | |
{ | |
_collection.Add(item); | |
} | |
} | |
public void Clear() | |
{ | |
lock (_syncRoot) | |
{ | |
_collection.Clear(); | |
} | |
} | |
public bool Contains(T item) | |
{ | |
lock (_syncRoot) | |
{ | |
return _collection.Contains(item); | |
} | |
} | |
public void CopyTo(T[] array, int arrayIndex) | |
{ | |
lock (_syncRoot) | |
{ | |
_collection.CopyTo(array, arrayIndex); | |
} | |
} | |
public bool Remove(T item) | |
{ | |
lock (_syncRoot) | |
{ | |
return _collection.Remove(item); | |
} | |
} | |
public int Count | |
{ | |
get | |
{ | |
lock (_syncRoot) | |
{ | |
return _collection.Count; | |
} | |
} | |
} | |
public bool IsReadOnly | |
{ | |
get | |
{ | |
lock (_syncRoot) | |
{ | |
return false; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment