Note
Use this class only if you want to wrap a generic ICollectionT instance to make it thread-safe.
If you want to use a thread-safe hash set optimized for concurrent operations consider to use the ThreadSafeHashSetT class instead.
[SerializableAttribute]
public class LockingCollection<T> : ICollection<T>,
IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>
<SerializableAttribute>
Public Class LockingCollection(Of T)
Implements ICollection(Of T), IEnumerable(Of T),
IEnumerable, IReadOnlyCollection(Of T)
[SerializableAttribute]
generic<typename T>
public ref class LockingCollection : ICollection<T>,
IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>
[<SerializableAttribute>]
type LockingCollection<'T> =
class
interface ICollection<'T>
interface IEnumerable<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
end
Thread safety means that all members of the underlying collection are accessed in a lock, which only provides that the collection remains consistent as long as it is accessed only by the members of this class. This does not solve every issue of multi-threading automatically. Consider the following example:
var asThreadSafe = new LockingCollection<MyClass>(myCollection);
// Though both calls use locks it still can happen that two threads add the same item twice this way
// because the lock is released between the two calls:
if (!asThreadSafe.Contains(myItem))
asThreadSafe.Add(myItem);
For the situations above a lock can be requested also explicitly by the Lock method, which can be released by the Unlock method. To release an explicitly requested lock the Unlock method must be called the same times as the Lock method. The fixed version of the example above:
var asThreadSafe = new LockingCollection<MyClass>(myCollection);
// This works well because the lock is not released between the two calls:
asThreadSafe.Lock();
try
{
if (!asThreadSafe.Contains(myItem))
asThreadSafe.Add(myItem);
}
finally
{
asThreadSafe.Unlock();
}
To avoid confusion, the non-generic ICollection interface is not implemented by the LockingCollectionT class because it uses a different aspect of synchronization.
The GetEnumerator method creates a snapshot of the underlying collection so obtaining the enumerator has an O(n) cost on this class.
LockingCollectionT | Initializes a new instance of the LockingCollectionT class with a HashSetT inside. |
LockingCollectionT(ICollectionT) | Initializes a new instance of the LockingCollectionT class. |
Count | Gets the number of elements contained in the LockingCollectionT. |
IsReadOnly | Gets a value indicating whether the LockingCollectionT is read-only. |
Add | Adds an item to the LockingCollectionT. |
Clear | Removes all items from the LockingCollectionT. |
Contains | Determines whether the LockingCollectionT contains a specific value. |
CopyTo | Copies the elements of the LockingCollectionT to an Array, starting at a particular arrayIndex. |
GetEnumerator | Returns an enumerator that iterates through the collection. |
Lock |
Locks the access of the underlying collection from other threads until Unlock is called as many times as this method was called. Needed to be called if
multiple calls to the wrapped collection have to be combined without releasing the lock between each calls.
See the Remarks section of the LockingCollectionT class for details and some examples. |
Remove | Removes the first occurrence of a specific object from the LockingCollectionT. |
Unlock | When called as many times as Lock was called previously, then unlocks the access of the underlying collection so other threads also can access it. |
AddRangeT |
Adds a collection to the target ICollectionT.
(Defined by CollectionExtensions) |
AsThreadSafeT |
Returns a LockingCollectionT, which provides a thread-safe wrapper for the specified collection.
This only means that if the members are accessed through the returned LockingCollectionT, then the inner state of the wrapped collection remains always consistent and not that all the multi-threading concerns can be ignored.
See the Remarks section of the LockingCollectionT class for details and some examples. (Defined by CollectionExtensions) |
Convert |
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example. (Defined by ObjectExtensions) |
ConvertTTarget |
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions) |
ForEachT |
Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions) |
GetRandomElementT |
Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions) |
GetRandomElementT |
Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions) |
In |
Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example. (Defined by ObjectExtensions) |
IndexOf |
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions) |
IndexOf |
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions) |
IndexOfT |
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions) |
IndexOfT |
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions) |
IsNullOrEmpty |
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions) |
IsNullOrEmptyT |
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions) |
JoinT |
Concatenates the items of the source collection into a new string instance
using the specified separator between the items.
(Defined by EnumerableExtensions) |
JoinT |
Concatenates the items of the source collection into a new string instance
using the specified separator between the items.
(Defined by EnumerableExtensions) |
ShuffleT |
Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions) |
ShuffleT |
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions) |
ShuffleT |
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions) |
ShuffleT |
Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions) |
ToCircularListT |
Creates a CircularListT from an IEnumerableT.
(Defined by EnumerableExtensions) |
ToStringKeyedDictionaryT |
Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified keySelector delegate and a comparer.
(Defined by EnumerableExtensions) |
ToStringKeyedDictionaryT, TValue |
Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified key and value selector delegates and a comparer.
(Defined by EnumerableExtensions) |
TryAdd |
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions) |
TryAddT |
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions) |
TryAddRange |
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions) |
TryAddRangeT |
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions) |
TryClear |
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions) |
TryClearT |
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions) |
TryConvert |
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryConvert |
Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryConvertTTarget |
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryConvertTTarget |
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. (Defined by ObjectExtensions) |
TryGetCount |
Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions) |
TryGetCountT |
Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions) |
TryGetElementAt |
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions) |
TryGetElementAtT |
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions) |
TryInsert |
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions) |
TryInsertT |
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions) |
TryInsertRange |
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions) |
TryInsertRangeT |
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions) |
TryRemove |
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions) |
TryRemoveT |
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions) |
TryRemoveAt |
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions) |
TryRemoveAtT |
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions) |
TryRemoveRange |
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions) |
TryRemoveRangeT |
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions) |
TryReplaceRange |
Tries to remove count amount of items from the target at the specified index, and
to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions) |
TryReplaceRangeT |
Tries to remove count amount of items from the target at the specified index, and
to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions) |
TrySetElementAt |
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions) |
TrySetElementAtT |
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions) |