語言

BlockingCollection<T>.TryTake 方法

定義

多載

名稱 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.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
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> 已經被處理掉了。

底層集合在此例外 BlockingCollection<T> 被修改。

備註

若集合為空,此方法會立即回傳 false。

移除項目的順序取決於建立實 BlockingCollection<T> 例所使用的集合類型。 建立物件時 BlockingCollection<T> ,你可以指定要使用的集合類型。 例如,你可以指定 ConcurrentQueue<T> 一個物件用於先進先出(FIFO)行為,或 ConcurrentStack<T> 一個物件用於後進先出(LIFO)行為。 您可以使用任何實作IProducerConsumerCollection<T>介面的集合類別。 預設集合類型 BlockingCollection<T> 為 ConcurrentQueue<T>。

另請參閱

適用於

TryTake(T, TimeSpan)

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
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 A 代表等待物品移除的毫秒數,或 TimeSpan A 代表無限等待的 -1 毫秒。

傳回

true若某項目能在指定時間內從收藏中移除;否則,。 false

例外狀況

這些 BlockingCollection<T> 已經被處理掉了。

timeout 是除 -1 毫秒外的負數,代表無限次逾時。

-或-

timeout 大於 Int32.MaxValue。

底層集合在此例外 BlockingCollection<T> 被修改。

備註

移除項目的順序取決於建立實 BlockingCollection<T> 例所使用的集合類型。 建立物件時 BlockingCollection<T> ,你可以指定要使用的集合類型。 例如,你可以指定 ConcurrentQueue<T> 一個物件用於先進先出(FIFO)行為,或 ConcurrentStack<T> 一個物件用於後進先出(LIFO)行為。 您可以使用任何實作IProducerConsumerCollection<T>介面的集合類別。 預設集合類型 BlockingCollection<T> 為 ConcurrentQueue<T>。

另請參閱

適用於

TryTake(T, Int32, CancellationToken)

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
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

例外狀況

BlockingCollection<T>已經被處置,或標的CancellationTokenSource資產已被處置。

millisecondsTimeout 是除 -1 以外的負數,而 -1 代表無限次超時。

底層集合在此實例外 BlockingCollection<T> 被修改。

備註

移除項目的順序取決於建立實 BlockingCollection<T> 例所使用的集合類型。 建立物件時 BlockingCollection<T> ,你可以指定要使用的集合類型。 例如,你可以指定 ConcurrentQueue<T> 一個物件用於先進先出(FIFO)行為,或 ConcurrentStack<T> 一個物件用於後進先出(LIFO)行為。 您可以使用任何實作IProducerConsumerCollection<T>介面的集合類別。 預設集合類型 BlockingCollection<T> 為 ConcurrentQueue<T>。

另請參閱

適用於

TryTake(T, Int32)

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
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

例外狀況

這些 BlockingCollection<T> 已經被處理掉了。

millisecondsTimeout 是除 -1 以外的負數,而 -1 代表無限次超時。

底層集合在此例外 BlockingCollection<T> 被修改。

備註

移除項目的順序取決於建立實 BlockingCollection<T> 例所使用的集合類型。 當你建立 時 BlockingCollection<T>,可以指定要使用的集合類型。 例如,你可以指定 ConcurrentQueue<T> 一個物件用於先進先出(FIFO)行為,或 ConcurrentStack<T> 一個物件用於後進先出(LIFO)行為。 您可以使用任何實作IProducerConsumerCollection<T>介面的集合類別。 預設集合類型 BlockingCollection<T> 為 ConcurrentQueue<T>。

另請參閱

適用於