다음을 통해 공유


BlockingCollection<T>.GetConsumingEnumerable 메서드

정의

컬렉션에 있는 항목에 사용하는 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>입니다.

예외

CancellationToken이 취소되었습니다.

BlockingCollection<T>이 삭제되었거나 cancellationToken을 만든 CancellationTokenSource가 삭제되었습니다.

설명

이 메서드를 사용하면 클라이언트 코드가 foreach 루프(Visual Basic의 For Each) 또는 Parallel.ForEach PLINQ 쿼리를 사용하여 컬렉션에서 항목을 제거할 수 있습니다. 열거자는 true를 반환할 때까지 IsCompleted 항목(있는 경우)을 계속 제공하고, 가 false이면 IsCompleted 항목을 사용할 수 있게 될 때까지 또는 가 취소될 때까지 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

추가 정보

적용 대상