BlockingCollection<T>.TryTake 方法

定義

嘗試從 BlockingCollection<T> 移除項目。Tries to remove an item from the BlockingCollection<T>.

多載

TryTake(T)

嘗試從 BlockingCollection<T> 移除項目。Tries to remove an item from the BlockingCollection<T>.

TryTake(T, TimeSpan)

嘗試在指定的時間週期內,從 BlockingCollection<T> 中移除項目。Tries to remove an item from the BlockingCollection<T> in the specified time period.

TryTake(T, Int32, CancellationToken)

當觀察取消語彙基元時,嘗試在指定的時間週期內,從 BlockingCollection<T> 中移除項目。Tries to remove an item from the BlockingCollection<T> in the specified time period while observing a cancellation token.

TryTake(T, Int32)

嘗試在指定的時間週期內,從 BlockingCollection<T> 中移除項目。Tries to remove an item from the BlockingCollection<T> in the specified time period.

範例

下列範例會示範如何使用 TryTake 方法。The following example shows how to use the TryTake method.

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);
        }
    }
}
'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> 移除項目。Tries to remove an item from the 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

要從集合中移除的項目。The item to be removed from the collection.

傳回

如果可以移除項目,則為 true,否則為 falsetrue if an item could be removed; otherwise, false.

例外狀況

基礎集合是在這個 BlockingCollection<T> 執行個體之外修改。The underlying collection was modified outside of this BlockingCollection<T> instance.

備註

如果集合是空的,則這個方法會立即傳回 false。If the collection is empty, this method immediately returns false.

項目的移除順序取決於用來建立 BlockingCollection<T> 執行個體的集合型別。The order in which an item is removed depends on the type of collection used to create the BlockingCollection<T> instance. 建立 BlockingCollection<T> 物件時,您可以指定要使用的集合型別。When you create a BlockingCollection<T> object, you can specify the type of collection to use. 例如,您可以針對先進先出 (FIFO) 行為指定 ConcurrentQueue<T> 物件,或針對後進先出 (LIFO) 行為指定 ConcurrentStack<T> 物件。For example, you could specify a ConcurrentQueue<T> object for first in, first out (FIFO) behavior, or a ConcurrentStack<T> object for last in, first out (LIFO) behavior. 您可以使用任何可實作 IProducerConsumerCollection<T> 介面的集合類別。You can use any collection class that implements the IProducerConsumerCollection<T> interface. BlockingCollection<T> 的預設集合類型為 ConcurrentQueue<T>The default collection type for BlockingCollection<T> is ConcurrentQueue<T>.

另請參閱

TryTake(T, TimeSpan)

嘗試在指定的時間週期內,從 BlockingCollection<T> 中移除項目。Tries to remove an item from the BlockingCollection<T> in the specified time period.

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

要從集合中移除的項目。The item to be removed from the collection.

timeout
TimeSpan

代表等候毫秒數的物件,或是代表無限期等候之 -1 毫秒的物件。An object that represents the number of milliseconds to wait, or an object that represents -1 milliseconds to wait indefinitely.

傳回

如果可在指定的時間內從集合中移除項目,則為 true,否則為 falsetrue if an item could be removed from the collection within the specified time; otherwise, false.

例外狀況

timeout 為 -1 毫秒以外的負數,表示無限逾時。timeout is a negative number other than -1 milliseconds, which represents an infinite time-out. -或--or- timeout 大於 MaxValuetimeout is greater than MaxValue.

基礎集合是在這個 BlockingCollection<T> 執行個體之外修改。The underlying collection was modified outside of this BlockingCollection<T> instance.

備註

項目的移除順序取決於用來建立 BlockingCollection<T> 執行個體的集合型別。The order in which an item is removed depends on the type of collection used to create the BlockingCollection<T> instance. 建立 BlockingCollection<T> 物件時,您可以指定要使用的集合型別。When you create a BlockingCollection<T> object, you can specify the type of collection to use. 例如,您可以針對先進先出 (FIFO) 行為指定 ConcurrentQueue<T> 物件,或針對後進先出 (LIFO) 行為指定 ConcurrentStack<T> 物件。For example, you could specify a ConcurrentQueue<T> object for first in, first out (FIFO) behavior, or a ConcurrentStack<T> object for last in, first out (LIFO) behavior. 您可以使用任何可實作 IProducerConsumerCollection<T> 介面的集合類別。You can use any collection class that implements the IProducerConsumerCollection<T> interface. BlockingCollection<T> 的預設集合類型為 ConcurrentQueue<T>The default collection type for BlockingCollection<T> is ConcurrentQueue<T>.

另請參閱

TryTake(T, Int32, CancellationToken)

當觀察取消語彙基元時,嘗試在指定的時間週期內,從 BlockingCollection<T> 中移除項目。Tries to remove an item from the BlockingCollection<T> in the specified time period while observing a cancellation token.

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

參數

item
T

要從集合中移除的項目。The item to be removed from the collection.

millisecondsTimeout
Int32

要等候的毫秒數,如果要無限期等候,則為 Infinite (-1)。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken

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

傳回

如果可在指定的時間內從集合中移除項目,則為 true,否則為 falsetrue if an item could be removed from the collection within the specified time; otherwise, false.

例外狀況

BlockingCollection<T> 已處置或基礎 CancellationTokenSource 已經過處置。The BlockingCollection<T> has been disposed or the underlying CancellationTokenSource has been disposed.

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

基礎集合曾在這個 BlockingCollection<T> 執行個體之外修改。The underlying collection was modified outside this BlockingCollection<T> instance.

備註

項目的移除順序取決於用來建立 BlockingCollection<T> 執行個體的集合型別。The order in which an item is removed depends on the type of collection used to create the BlockingCollection<T> instance. 建立 BlockingCollection<T> 物件時,您可以指定要使用的集合型別。When you create a BlockingCollection<T> object, you can specify the type of collection to use. 例如,您可以針對先進先出 (FIFO) 行為指定 ConcurrentQueue<T> 物件,或針對後進先出 (LIFO) 行為指定 ConcurrentStack<T> 物件。For example, you could specify a ConcurrentQueue<T> object for first in, first out (FIFO) behavior, or a ConcurrentStack<T> object for last in, first out (LIFO) behavior. 您可以使用任何可實作 IProducerConsumerCollection<T> 介面的集合類別。You can use any collection class that implements the IProducerConsumerCollection<T> interface. BlockingCollection<T> 的預設集合類型為 ConcurrentQueue<T>The default collection type for BlockingCollection<T> is ConcurrentQueue<T>.

另請參閱

TryTake(T, Int32)

嘗試在指定的時間週期內,從 BlockingCollection<T> 中移除項目。Tries to remove an item from the BlockingCollection<T> in the specified time period.

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

要從集合中移除的項目。The item to be removed from the collection.

millisecondsTimeout
Int32

要等候的毫秒數,如果要無限期等候,則為 Infinite (-1)。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

傳回

如果可在指定的時間內從集合中移除項目,則為 true,否則為 falsetrue if an item could be removed from the collection within the specified time; otherwise, false.

例外狀況

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

基礎集合是在這個 BlockingCollection<T> 執行個體之外修改。The underlying collection was modified outside of this BlockingCollection<T> instance.

備註

項目的移除順序取決於用來建立 BlockingCollection<T> 執行個體的集合型別。The order in which an item is removed depends on the type of collection used to create the BlockingCollection<T> instance. 當您建立 BlockingCollection<T>時,可以指定要使用的集合類型。When you create a BlockingCollection<T>, you can specify the type of collection to use. 例如,您可以針對先進先出 (FIFO) 行為指定 ConcurrentQueue<T> 物件,或針對後進先出 (LIFO) 行為指定 ConcurrentStack<T> 物件。For example, you could specify a ConcurrentQueue<T> object for first in, first out (FIFO) behavior, or a ConcurrentStack<T> object for last in, first out (LIFO) behavior. 您可以使用任何可實作 IProducerConsumerCollection<T> 介面的集合類別。You can use any collection class that implements the IProducerConsumerCollection<T> interface. BlockingCollection<T> 的預設集合類型為 ConcurrentQueue<T>The default collection type for BlockingCollection<T> is ConcurrentQueue<T>.

另請參閱

適用於