3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
8 // IProducerConsumerCollection.cs
10 // <OWNER>Microsoft</OWNER>
12 // A common interface for all concurrent collections.
14 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
16 using System.Collections;
17 using System.Collections.Generic;
18 using System.Diagnostics;
20 namespace System.Collections.Concurrent
24 /// Defines methods to manipulate thread-safe collections intended for producer/consumer usage.
26 /// <typeparam name="T">Specifies the type of elements in the collection.</typeparam>
28 /// All implementations of this interface must enable all members of this interface
29 /// to be used concurrently from multiple threads.
31 public interface IProducerConsumerCollection<T> : IEnumerable<T>, ICollection
35 /// Copies the elements of the <see cref="IProducerConsumerCollection{T}"/> to
37 /// <see cref="T:System.Array"/>, starting at a specified index.
39 /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of
40 /// the elements copied from the <see cref="IProducerConsumerCollection{T}"/>.
41 /// The array must have zero-based indexing.</param>
42 /// <param name="index">The zero-based index in <paramref name="array"/> at which copying
44 /// <exception cref="ArgumentNullException"><paramref name="array"/> is a null reference (Nothing in
45 /// Visual Basic).</exception>
46 /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than
48 /// <exception cref="ArgumentException"><paramref name="index"/> is equal to or greater than the
49 /// length of the <paramref name="array"/>
50 /// -or- The number of elements in the source <see cref="ConcurrentQueue{T}"/> is greater than the
51 /// available space from <paramref name="index"/> to the end of the destination <paramref
54 void CopyTo(T[] array, int index);
57 /// Attempts to add an object to the <see
58 /// cref="IProducerConsumerCollection{T}"/>.
60 /// <param name="item">The object to add to the <see
61 /// cref="IProducerConsumerCollection{T}"/>.</param>
62 /// <returns>true if the object was added successfully; otherwise, false.</returns>
63 /// <exception cref="T:System.ArgumentException">The <paramref name="item"/> was invalid for this collection.</exception>
67 /// Attempts to remove and return an object from the <see cref="IProducerConsumerCollection{T}"/>.
69 /// <param name="item">
70 /// When this method returns, if the object was removed and returned successfully, <paramref
71 /// name="item"/> contains the removed object. If no object was available to be removed, the value is
74 /// <returns>true if an object was removed and returned successfully; otherwise, false.</returns>
75 bool TryTake(out T item);
78 /// Copies the elements contained in the <see cref="IProducerConsumerCollection{T}"/> to a new array.
80 /// <returns>A new array containing the elements copied from the <see cref="IProducerConsumerCollection{T}"/>.</returns>
87 /// A debugger view of the IProducerConsumerCollection that makes it simple to browse the
88 /// collection's contents at a point in time.
90 /// <typeparam name="T">The type of elements stored within.</typeparam>
91 internal sealed class SystemCollectionsConcurrent_ProducerConsumerCollectionDebugView<T>
93 private IProducerConsumerCollection<T> m_collection; // The collection being viewed.
96 /// Constructs a new debugger view object for the provided collection object.
98 /// <param name="collection">A collection to browse in the debugger.</param>
99 public SystemCollectionsConcurrent_ProducerConsumerCollectionDebugView(IProducerConsumerCollection<T> collection)
101 if (collection == null)
103 throw new ArgumentNullException("collection");
106 m_collection = collection;
110 /// Returns a snapshot of the underlying collection's elements.
112 [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
115 get { return m_collection.ToArray(); }