BlockingCollection<T>.GetConsumingEnumerable BlockingCollection<T>.GetConsumingEnumerable BlockingCollection<T>.GetConsumingEnumerable BlockingCollection<T>.GetConsumingEnumerable Method

Definition

Proporciona una interfaz IEnumerator<T> en uso para los elementos de la colección. Provides a consuming IEnumerator<T> for items in the collection.

Overloads

GetConsumingEnumerable(CancellationToken) GetConsumingEnumerable(CancellationToken) GetConsumingEnumerable(CancellationToken)

Proporciona una interfaz IEnumerable<T> en uso para los elementos de la colección. Provides a consuming IEnumerable<T> for items in the collection.

GetConsumingEnumerable() GetConsumingEnumerable() GetConsumingEnumerable() GetConsumingEnumerable()

Proporciona una interfaz IEnumerator<T> en uso para los elementos de la colección. Provides a consuming IEnumerator<T> for items in the collection.

GetConsumingEnumerable(CancellationToken) GetConsumingEnumerable(CancellationToken) GetConsumingEnumerable(CancellationToken)

Proporciona una interfaz IEnumerable<T> en uso para los elementos de la colección. Provides a consuming IEnumerable<T> for items in the collection.

public:
 System::Collections::Generic::IEnumerable<T> ^ GetConsumingEnumerable(System::Threading::CancellationToken cancellationToken);
public System.Collections.Generic.IEnumerable<T> GetConsumingEnumerable (System.Threading.CancellationToken cancellationToken);
member this.GetConsumingEnumerable : System.Threading.CancellationToken -> seq<'T>

Parámetros

cancellationToken
CancellationToken CancellationToken CancellationToken CancellationToken

Token de cancelación que se va a observar. A cancellation token to observe.

Returns

IEnumerable<T> que quita y devuelve elementos de la colección. An IEnumerable<T> that removes and returns items from the collection.

Exceptions

BlockingCollection<T> ha sido eliminado o CancellationTokenSource que creó cancellationToken ha sido eliminado. The BlockingCollection<T> has been disposed or the CancellationTokenSource that created cancellationToken has been disposed

Remarks

Este método permite que el código de cliente quitar elementos de la colección mediante el uso de un bucle foreach (para cada en Visual Basic), o Parallel.ForEach o una consulta PLINQ.This method enables client code to remove items from the collection by using a foreach loop (For Each in Visual Basic), or Parallel.ForEach or a PLINQ query. El enumerador seguirá proporcionando elementos (si existe alguno) hasta IsCompleted devuelve true y si IsCompleted es false el bucle se bloquea hasta que haya disponible un artículo o hasta que el CancellationToken se ha cancelado.The enumerator will continue to provide items (if any exist) until IsCompleted returns true, and if IsCompleted is false the loop blocks until an item becomes available or until the CancellationToken is cancelled.

See Also

GetConsumingEnumerable() GetConsumingEnumerable() GetConsumingEnumerable() GetConsumingEnumerable()

Proporciona una interfaz IEnumerator<T> en uso para los elementos de la colección. Provides a consuming IEnumerator<T> for items in the collection.

public:
 System::Collections::Generic::IEnumerable<T> ^ GetConsumingEnumerable();
public System.Collections.Generic.IEnumerable<T> GetConsumingEnumerable ();
member this.GetConsumingEnumerable : unit -> seq<'T>
Public Function GetConsumingEnumerable () As IEnumerable(Of T)

Returns

IEnumerable<T> que quita y devuelve elementos de la colección. An IEnumerable<T> that removes and returns items from the collection.

Exceptions

Examples

El ejemplo siguiente muestra cómo usar el GetConsumingEnumerable método:The following example shows how to use the GetConsumingEnumerable method:

class ConsumingEnumerableDemo
{
    // Demonstrates:
    //      BlockingCollection<T>.Add()
    //      BlockingCollection<T>.CompleteAdding()
    //      BlockingCollection<T>.GetConsumingEnumerable()
    public static void BC_GetConsumingEnumerable()
    {
        using (BlockingCollection<int> bc = new BlockingCollection<int>())
        {

            // Kick off a producer task
            Task.Factory.StartNew(async () =>
            {
                for (int i = 0; i < 10; i++)
                {
                    bc.Add(i);
                    await Task.Delay(100); // sleep 100 ms between adds
                }

                // Need to do this to keep foreach below from hanging
                bc.CompleteAdding();
            });

            // Now consume the blocking collection with foreach.
            // Use bc.GetConsumingEnumerable() instead of just bc because the
            // former will block waiting for completion and the latter will
            // simply take a snapshot of the current state of the underlying collection.
            foreach (var item in bc.GetConsumingEnumerable())
            {
                Console.WriteLine(item);
            }
        }
    }
}
'Imports System.Threading.Tasks
'Imports System.Collections.Concurrent

' Demonstrates:
' BlockingCollection<T>.Add()
' BlockingCollection<T>.CompleteAdding()
' BlockingCollection<T>.GetConsumingEnumerable()

Class ConsumingEnumerableDemo
    Shared Sub BC_GetConsumingEnumerable()
        Using bc As New BlockingCollection(Of Integer)()

            ' Kick off a producer task
            Task.Factory.StartNew(
                Sub()
                    For i As Integer = 0 To 9
                        bc.Add(i)
                        ' sleep 100 ms between adds
                        Thread.Sleep(100)
                    Next

                    ' Need to do this to keep foreach below from hanging
                    bc.CompleteAdding()
                End Sub)
            ' Now consume the blocking collection with foreach.
            ' Use bc.GetConsumingEnumerable() instead of just bc because the
            ' former will block waiting for completion and the latter will
            ' simply take a snapshot of the current state of the underlying collection.
            For Each item In bc.GetConsumingEnumerable()
                Console.WriteLine(item)
            Next
        End Using
    End Sub
End Class
See Also

Applies to