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)

尝试从中 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> 对象,也可以 ConcurrentStack<T> 指定最后一个先出 (LIFO) 行为的对象。 可以使用任何集合类,这些集合类实现了 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 毫秒,表示无限超时。

-或-

timeout 大于 Int32.MaxValue

基础集合在此实例之外 BlockingCollection<T> 进行了修改。

注解

删除项的顺序取决于用于创建 BlockingCollection<T> 实例的集合的类型。 创建 BlockingCollection<T> 对象时,可以指定要使用的集合类型。 例如,可以为先入、先出(FIFO)行为指定一个 ConcurrentQueue<T> 对象,也可以 ConcurrentStack<T> 指定最后一个先出 (LIFO) 行为的对象。 可以使用任何集合类,这些集合类实现了 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.

例外

millisecondsTimeout 是非 -1 的负数,表示无限超时。

基础集合在此实例外部 BlockingCollection<T> 进行了修改。

注解

删除项的顺序取决于用于创建 BlockingCollection<T> 实例的集合的类型。 创建 BlockingCollection<T> 对象时,可以指定要使用的集合类型。 例如,可以为先入、先出(FIFO)行为指定一个 ConcurrentQueue<T> 对象,也可以 ConcurrentStack<T> 指定最后一个先出 (LIFO) 行为的对象。 可以使用任何集合类,这些集合类实现了 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> 对象,也可以 ConcurrentStack<T> 指定最后一个先出 (LIFO) 行为的对象。 可以使用任何集合类,这些集合类实现了 IProducerConsumerCollection<T> 接口。 的默认集合类型 BlockingCollection<T>ConcurrentQueue<T>.

另请参阅

适用于