BlockingCollection<T>.GetConsumingEnumerable 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为集合中的项提供一个使用 IEnumerable<T>。
重载
| GetConsumingEnumerable(CancellationToken) | 为集合中的项提供一个使用 IEnumerable<T>。 | 
| GetConsumingEnumerable() | 为集合中的项提供一个使用 IEnumerable<T>。 | 
GetConsumingEnumerable(CancellationToken)
- Source:
- BlockingCollection.cs
- Source:
- BlockingCollection.cs
- Source:
- BlockingCollection.cs
为集合中的项提供一个使用 IEnumerable<T>。
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>Public Function GetConsumingEnumerable (cancellationToken As CancellationToken) As IEnumerable(Of T)Public Iterator Function GetConsumingEnumerable (cancellationToken As CancellationToken) As IEnumerable(Of T)参数
- cancellationToken
- CancellationToken
要观察的取消标记。
返回
从集合中移除并返回项的 IEnumerable<T>。
例外
已释放 BlockingCollection<T>,或已释放创建 cancellationToken 的 CancellationTokenSource
注解
此方法使客户端代码能够使用 foreach 循环 (Visual Basic) 中的 Foreach 循环或 Parallel.ForEach PLINQ 查询从集合中删除项。 如果存在任何项,枚举器将继续提供项 () 直到 IsCompleted 返回 true;如果 IsCompleted 为 false,则循环将阻止,直到项变得可用或 CancellationToken 取消。
另请参阅
适用于
GetConsumingEnumerable()
- Source:
- BlockingCollection.cs
- Source:
- BlockingCollection.cs
- Source:
- BlockingCollection.cs
为集合中的项提供一个使用 IEnumerable<T>。
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)返回
从集合中移除并返回项的 IEnumerable<T>。
例外
示例
下面的示例显示如何使用 GetConsumingEnumerable 方法:
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
        }
    }
}
module ConsumingEnumerableDemo =
    // Demonstrates:
    //      BlockingCollection<T>.Add()
    //      BlockingCollection<T>.CompleteAdding()
    //      BlockingCollection<T>.GetConsumingEnumerable()
    let blockingCollectionGetConsumingEnumerable () =
        task {
            use bc = new BlockingCollection<int>()
            // Kick off a producer task
            let producerTask =
                task {
                    for i = 0 to 9 do
                        bc.Add i
                        printfn $"Producing: {i}"
                        do! 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.
            for item in bc.GetConsumingEnumerable() do
                printfn $"Consuming: {item}"
            do! producerTask // Allow task to complete cleanup
        }
'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 not responding.
                    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