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>
Public Function GetConsumingEnumerable (cancellationToken As CancellationToken) As IEnumerable(Of T)
Public Iterator Function GetConsumingEnumerable (cancellationToken As CancellationToken) As IEnumerable(Of T)

매개 변수

cancellationToken
CancellationToken

관찰할 취소 토큰입니다.A cancellation token to observe.

반환

IEnumerable<T>

컬렉션에서 항목을 제거하고 반환하는 IEnumerable<T>입니다.An IEnumerable<T> that removes and returns items from the collection.

예외

CancellationToken이 취소된 경우.If the CancellationToken is canceled.

BlockingCollection<T>이 삭제되었거나 cancellationToken을 만든 CancellationTokenSource가 삭제되었습니다.The BlockingCollection<T> has been disposed or the CancellationTokenSource that created cancellationToken has been disposed

설명

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

컬렉션에서 항목을 제거하고 반환하는 IEnumerable<T>입니다.An IEnumerable<T> that removes and returns items from the collection.

예외

BlockingCollection<T>가 삭제된 경우The BlockingCollection<T> has been disposed.

예제

다음 예제에서는 메서드를 사용 하는 방법을 보여 줍니다 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
            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
        }
    }
}
'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

적용 대상