BlockingCollection<T>.GetConsumingEnumerable 方法

定義

提供集合中項目的耗用 IEnumerator<T>Provides a consuming IEnumerator<T> for items in the collection.

多載

GetConsumingEnumerable(CancellationToken)

提供集合中項目的耗用 IEnumerable<T>Provides a consuming IEnumerable<T> for items in the collection.

GetConsumingEnumerable()

提供集合中項目的耗用 IEnumerator<T>Provides a consuming IEnumerator<T> for items in the collection.

GetConsumingEnumerable(CancellationToken)

提供集合中項目的耗用 IEnumerable<T>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>

參數

cancellationToken
CancellationToken

要觀察的取消語彙基元。A cancellation token to observe.

傳回

IEnumerable<T>,其會移除和傳回集合中的項目。An IEnumerable<T> that removes and returns items from the collection.

例外狀況

已處置 BlockingCollection<T> 或建立 CancellationTokenSourcecancellationTokenThe BlockingCollection<T> has been disposed or the CancellationTokenSource that created cancellationToken has been disposed

備註

這個方法可讓用戶端程式代碼使用 foreach 迴圈(適用于 Visual Basic 中的每個),或 Parallel.ForEach 或 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. 列舉值會繼續提供專案(如果有的話),直到 IsCompleted 傳回 true 為止; 如果 IsCompleted 為 false,則迴圈會封鎖直到專案可供使用,或直到 CancellationToken 取消為止。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.

另請參閱

GetConsumingEnumerable()

提供集合中項目的耗用 IEnumerator<T>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)

傳回

IEnumerable<T>,其會移除和傳回集合中的項目。An IEnumerable<T> that removes and returns items from the collection.

例外狀況

範例

下列範例顯示如何使用 GetConsumingEnumerable 方法:The following example shows how to use the GetConsumingEnumerable method:

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
            await Task.Run(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 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

另請參閱

適用於