BlockingCollection<T>.GetConsumingEnumerable Method

Definition

Provides a consuming IEnumerable<T> for items in the collection.

Overloads

GetConsumingEnumerable(CancellationToken)

Provides a consuming IEnumerable<T> for items in the collection.

GetConsumingEnumerable()

Provides a consuming IEnumerable<T> for items in the collection.

GetConsumingEnumerable(CancellationToken)

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

Provides a consuming IEnumerable<T> for items in the collection.

C#
public System.Collections.Generic.IEnumerable<T> GetConsumingEnumerable(System.Threading.CancellationToken cancellationToken);

Parameters

cancellationToken
CancellationToken

A cancellation token to observe.

Returns

An IEnumerable<T> that removes and returns items from the collection.

Exceptions

The BlockingCollection<T> has been disposed or the CancellationTokenSource that created cancellationToken has been disposed

Remarks

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. 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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetConsumingEnumerable()

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

Provides a consuming IEnumerable<T> for items in the collection.

C#
public System.Collections.Generic.IEnumerable<T> GetConsumingEnumerable();

Returns

An IEnumerable<T> that removes and returns items from the collection.

Exceptions

Examples

The following example shows how to use the GetConsumingEnumerable method:

C#
class ConsumingEnumerableDemo
{
    // Demonstrates:
    //      BlockingCollection<T>.Add()
    //      BlockingCollection<T>.CompleteAdding()
    //      BlockingCollection<T>.GetConsumingEnumerable()
    public static async Task BC_GetConsumingEnumerable()
    {
        using (BlockingCollection<int> bc = new BlockingCollection<int>())
        {
            // Kick off a producer task
            var producerTask = Task.Run(async () =>
            {
                for (int i = 0; i < 10; i++)
                {
                    bc.Add(i);
                    Console.WriteLine($"Producing: {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($"Consuming: {item}");
            }
            await producerTask; // Allow task to complete cleanup
        }
    }
}

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0