BlockingCollection<T>.TryTake 메서드

정의

에서 BlockingCollection<T>항목을 제거하려고 합니다.

오버로드

Name Description
TryTake(T)

에서 BlockingCollection<T>항목을 제거하려고 합니다.

TryTake(T, TimeSpan)

지정된 기간 동안 항목을 BlockingCollection<T> 제거하려고 시도합니다.

TryTake(T, Int32, CancellationToken)

취소 토큰을 관찰하는 동안 지정된 기간 동안 항목을 BlockingCollection<T> 제거하려고 시도합니다.

TryTake(T, Int32)

지정된 기간 동안 항목을 BlockingCollection<T> 제거하려고 시도합니다.

예제

다음 예제에서는 메서드를 사용하는 TryTake 방법을 보여줍니다.

class TryTakeDemo
{
    // Demonstrates:
    //      BlockingCollection<T>.Add()
    //      BlockingCollection<T>.CompleteAdding()
    //      BlockingCollection<T>.TryTake()
    //      BlockingCollection<T>.IsCompleted
    public static void BC_TryTake()
    {
        // Construct and fill our BlockingCollection
        using (BlockingCollection<int> bc = new BlockingCollection<int>())
        {
            int NUMITEMS = 10000;
            for (int i = 0; i < NUMITEMS; i++) bc.Add(i);
            bc.CompleteAdding();
            int outerSum = 0;

            // Delegate for consuming the BlockingCollection and adding up all items
            Action action = () =>
            {
                int localItem;
                int localSum = 0;

                while (bc.TryTake(out localItem)) localSum += localItem;
                Interlocked.Add(ref outerSum, localSum);
            };

            // Launch three parallel actions to consume the BlockingCollection
            Parallel.Invoke(action, action, action);

            Console.WriteLine("Sum[0..{0}) = {1}, should be {2}", NUMITEMS, outerSum, ((NUMITEMS * (NUMITEMS - 1)) / 2));
            Console.WriteLine("bc.IsCompleted = {0} (should be true)", bc.IsCompleted);
        }
    }
}
module TryTakeDemo =
    // Demonstrates:
    //      BlockingCollection<T>.Add()
    //      BlockingCollection<T>.CompleteAdding()
    //      BlockingCollection<T>.TryTake()
    //      BlockingCollection<T>.IsCompleted
    let blockingCollectionTryTake () =
        // Construct and fill our BlockingCollection
        use bc = new BlockingCollection<int>()
        let NUMITEMS = 10000;
        for i = 0 to NUMITEMS - 1 do
            bc.Add i
        bc.CompleteAdding()
        let mutable outerSum = 0

        // Delegate for consuming the BlockingCollection and adding up all items
        let action = 
            Action(fun () ->
                let mutable localItem = 0
                let mutable localSum = 0

                while bc.TryTake &localItem do
                    localSum <- localSum + localItem
                Interlocked.Add(&outerSum, localSum)
                |> ignore)

        // Launch three parallel actions to consume the BlockingCollection
        Parallel.Invoke(action, action, action)

        printfn $"Sum[0..{NUMITEMS}) = {outerSum}, should be {((NUMITEMS * (NUMITEMS - 1)) / 2)}"
        printfn $"bc.IsCompleted = {bc.IsCompleted} (should be true)"
'Imports System.Collections.Concurrent
'Imports System.Threading
'Imports System.Threading.Tasks

Class TryTakeDemo
    ' Demonstrates:
    ' BlockingCollection<T>.Add()
    ' BlockingCollection<T>.CompleteAdding()
    ' BlockingCollection<T>.TryTake()
    ' BlockingCollection<T>.IsCompleted
    Shared Sub BC_TryTake()
        ' Construct and fill our BlockingCollection
        Using bc As New BlockingCollection(Of Integer)()
            Dim NUMITEMS As Integer = 10000
            For i As Integer = 0 To NUMITEMS - 1
                bc.Add(i)
            Next
            bc.CompleteAdding()
            Dim outerSum As Integer = 0

            ' Delegate for consuming the BlockingCollection and adding up all items
            Dim action As Action =
                Sub()
                    Dim localItem As Integer
                    Dim localSum As Integer = 0

                    While bc.TryTake(localItem)
                        localSum += localItem
                    End While
                    Interlocked.Add(outerSum, localSum)
                End Sub

            ' Launch three parallel actions to consume the BlockingCollection
            Parallel.Invoke(action, action, action)

            Console.WriteLine("Sum[0..{0}) = {1}, should be {2}", NUMITEMS, outerSum, ((NUMITEMS * (NUMITEMS - 1)) / 2))
            Console.WriteLine("bc.IsCompleted = {0} (should be true)", bc.IsCompleted)
        End Using
    End Sub

End Class

TryTake(T)

에서 BlockingCollection<T>항목을 제거하려고 합니다.

public:
 bool TryTake([Runtime::InteropServices::Out] T % item);
public bool TryTake(out T item);
member this.TryTake : 'T -> bool
Public Function TryTake (ByRef item As T) As Boolean

매개 변수

item
T

컬렉션에서 제거할 항목입니다.

반품

true항목을 제거할 수 있으면 이고, 그렇지 않으면 . false

예외

기본 컬렉션이 이 BlockingCollection<T> 인스턴스 외부에서 수정되었습니다.

설명

컬렉션이 비어 있으면 이 메서드는 즉시 false를 반환합니다.

항목이 제거되는 순서는 인스턴스를 만드는 BlockingCollection<T> 데 사용되는 컬렉션 유형에 따라 달라집니다. 개체를 BlockingCollection<T> 만들 때 사용할 컬렉션 유형을 지정할 수 있습니다. 예를 들어 FIFO(선입선행) 동작에 대한 개체 또는 ConcurrentQueue<T> LIFO(Last in, first out) 동작에 대한 개체를 지정할 ConcurrentStack<T> 수 있습니다. IProducerConsumerCollection<T> 인터페이스를 구현하는 모든 컬렉션 클래스를 사용할 수 있습니다. 기본 컬렉션 유형은 BlockingCollection<T>이며, ConcurrentQueue<T>입니다.

추가 정보

적용 대상

TryTake(T, TimeSpan)

지정된 기간 동안 항목을 BlockingCollection<T> 제거하려고 시도합니다.

public:
 bool TryTake([Runtime::InteropServices::Out] T % item, TimeSpan timeout);
public bool TryTake(out T item, TimeSpan timeout);
member this.TryTake : 'T * TimeSpan -> bool
Public Function TryTake (ByRef item As T, timeout As TimeSpan) As Boolean

매개 변수

item
T

컬렉션에서 제거할 항목입니다.

timeout
TimeSpan

TimeSpan 항목이 제거될 때까지 대기할 시간(밀리초) 또는 TimeSpan 무기한 대기할 -1 밀리초를 나타내는 값입니다.

반품

true지정된 시간 내에 컬렉션에서 항목을 제거할 수 있으면 이고, 그렇지 않으면 . false

예외

timeout 는 -1 밀리초 이외의 음수로, 무한 제한 시간을 나타냅니다.

-또는-

timeoutInt32.MaxValue보다 큽다.

기본 컬렉션이 이 BlockingCollection<T> 인스턴스 외부에서 수정되었습니다.

설명

항목이 제거되는 순서는 인스턴스를 만드는 BlockingCollection<T> 데 사용되는 컬렉션 유형에 따라 달라집니다. 개체를 BlockingCollection<T> 만들 때 사용할 컬렉션 유형을 지정할 수 있습니다. 예를 들어 FIFO(선입선행) 동작에 대한 개체 또는 ConcurrentQueue<T> LIFO(Last in, first out) 동작에 대한 개체를 지정할 ConcurrentStack<T> 수 있습니다. IProducerConsumerCollection<T> 인터페이스를 구현하는 모든 컬렉션 클래스를 사용할 수 있습니다. 기본 컬렉션 유형은 BlockingCollection<T>이며, ConcurrentQueue<T>입니다.

추가 정보

적용 대상

TryTake(T, Int32, CancellationToken)

취소 토큰을 관찰하는 동안 지정된 기간 동안 항목을 BlockingCollection<T> 제거하려고 시도합니다.

public:
 bool TryTake([Runtime::InteropServices::Out] T % item, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public bool TryTake(out T item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
member this.TryTake : 'T * int * System.Threading.CancellationToken -> bool
Public Function TryTake (ByRef item As T, millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Boolean

매개 변수

item
T

컬렉션에서 제거할 항목입니다.

millisecondsTimeout
Int32

항목이 제거 Infinite 될 때까지 대기하거나(-1) 무기한 대기할 때까지 대기할 시간(밀리초)입니다.

cancellationToken
CancellationToken

관찰할 취소 토큰입니다.

반품

true지정된 시간 내에 컬렉션에서 항목을 제거할 수 있으면 이고, 그렇지 않으면 . false

예외

BlockingCollection<T> 삭제되었거나 기본 CancellationTokenSource 이 삭제되었습니다.

millisecondsTimeout 는 무한 제한 시간을 나타내는 -1 이외의 음수입니다.

기본 컬렉션이 이 BlockingCollection<T> 인스턴스 외부에서 수정되었습니다.

설명

항목이 제거되는 순서는 인스턴스를 만드는 BlockingCollection<T> 데 사용되는 컬렉션 유형에 따라 달라집니다. 개체를 BlockingCollection<T> 만들 때 사용할 컬렉션 유형을 지정할 수 있습니다. 예를 들어 FIFO(선입선행) 동작에 대한 개체 또는 ConcurrentQueue<T> LIFO(Last in, first out) 동작에 대한 개체를 지정할 ConcurrentStack<T> 수 있습니다. IProducerConsumerCollection<T> 인터페이스를 구현하는 모든 컬렉션 클래스를 사용할 수 있습니다. 기본 컬렉션 유형은 BlockingCollection<T>이며, ConcurrentQueue<T>입니다.

추가 정보

적용 대상

TryTake(T, Int32)

지정된 기간 동안 항목을 BlockingCollection<T> 제거하려고 시도합니다.

public:
 bool TryTake([Runtime::InteropServices::Out] T % item, int millisecondsTimeout);
public bool TryTake(out T item, int millisecondsTimeout);
member this.TryTake : 'T * int -> bool
Public Function TryTake (ByRef item As T, millisecondsTimeout As Integer) As Boolean

매개 변수

item
T

컬렉션에서 제거할 항목입니다.

millisecondsTimeout
Int32

항목이 제거 Infinite 될 때까지 대기하거나(-1) 무기한 대기할 때까지 대기할 시간(밀리초)입니다.

반품

true지정된 시간 내에 컬렉션에서 항목을 제거할 수 있으면 이고, 그렇지 않으면 . false

예외

millisecondsTimeout 는 무한 제한 시간을 나타내는 -1 이외의 음수입니다.

기본 컬렉션이 이 BlockingCollection<T> 인스턴스 외부에서 수정되었습니다.

설명

항목이 제거되는 순서는 인스턴스를 만드는 BlockingCollection<T> 데 사용되는 컬렉션 유형에 따라 달라집니다. 만들 BlockingCollection<T>때 사용할 컬렉션 유형을 지정할 수 있습니다. 예를 들어 FIFO(선입선행) 동작에 대한 개체 또는 ConcurrentQueue<T> LIFO(Last in, first out) 동작에 대한 개체를 지정할 ConcurrentStack<T> 수 있습니다. IProducerConsumerCollection<T> 인터페이스를 구현하는 모든 컬렉션 클래스를 사용할 수 있습니다. 기본 컬렉션 유형은 BlockingCollection<T>이며, ConcurrentQueue<T>입니다.

추가 정보

적용 대상