BlockingCollection<T>.GetConsumingEnumerable Method (CancellationToken)

.NET Framework (current version)

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

Namespace:   System.Collections.Concurrent
Assembly:  System (in System.dll)

public IEnumerable<T> GetConsumingEnumerable(
	CancellationToken cancellationToken


Type: System.Threading.CancellationToken

A cancellation token to observe.

Return Value

Type: System.Collections.Generic.IEnumerable<T>

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

Exception Condition

If the CancellationToken is canceled.


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

This method enables client code to remove items from the collection by using a foreach loop (For Each in Visual Basic), or Parallel.ForEach<TSource> 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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top