BlockingCollection<T>.TryTake 메서드

정의

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

오버로드

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)

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

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를 반환합니다.

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

추가 정보

적용 대상

TryTake(T, TimeSpan)

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

지정된 시간 안에 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> 인스턴스 외부에서 수정되었습니다.

설명

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

추가 정보

적용 대상

TryTake(T, Int32, CancellationToken)

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

취소 토큰을 관찰하는 동안 지정된 시간 안에 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.

예외

CancellationToken이 취소되었습니다.

BlockingCollection<T>이 삭제되었거나 내부 CancellationTokenSource가 삭제되었습니다.

millisecondsTimeout이 시간 제한 없음을 나타내는 -1 이외의 음수인 경우

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

설명

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

추가 정보

적용 대상

TryTake(T, Int32)

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

지정된 시간 안에 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> 인스턴스 외부에서 수정되었습니다.

설명

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

추가 정보

적용 대상