BlockingCollection<T>.TryTake 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
尝试从 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 -> boolPublic Function TryTake (ByRef item As T) As Boolean参数
- item
- T
要从集合中移除的项。
返回
如果可以移除项,则为 true;否则为 false。
例外
该基础集合已在此 BlockingCollection<T> 实例外部进行了修改。
注解
如果集合为空,此方法将立即返回 false。
删除项的顺序取决于用于创建实例的 BlockingCollection<T> 集合类型。 创建 BlockingCollection<T> 对象时,可以指定要使用的集合类型。 例如,可以为先入先出 (FIFO) 行为指定对象,或者ConcurrentStack<T>为后进先出 (LIFO) 行为指定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 -> boolPublic Function TryTake (ByRef item As T, timeout As TimeSpan) As Boolean参数
- item
- T
要从集合中移除的项。
返回
              true 如果可以在指定时间内从集合中删除项,则为 ;否则为 false。
例外
该基础集合已在此 BlockingCollection<T> 实例外部进行了修改。
注解
删除项的顺序取决于用于创建实例的 BlockingCollection<T> 集合类型。 创建 BlockingCollection<T> 对象时,可以指定要使用的集合类型。 例如,可以为先入先出 (FIFO) 行为指定对象,或者ConcurrentStack<T>为后进先出 (LIFO) 行为指定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 -> boolPublic Function TryTake (ByRef item As T, millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Boolean参数
- item
- T
要从集合中移除的项。
- cancellationToken
- CancellationToken
要观察的取消标记。
返回
              true 如果可以在指定时间内从集合中删除项,则为 ;否则为 false。
例外
              millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。
该基础集合已在此 BlockingCollection<T> 实例外部进行了修改。
注解
删除项的顺序取决于用于创建实例的 BlockingCollection<T> 集合类型。 创建 BlockingCollection<T> 对象时,可以指定要使用的集合类型。 例如,可以为先入先出 (FIFO) 行为指定对象,或者ConcurrentStack<T>为后进先出 (LIFO) 行为指定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 -> boolPublic Function TryTake (ByRef item As T, millisecondsTimeout As Integer) As Boolean参数
- item
- T
要从集合中移除的项。
返回
              true 如果可以在指定时间内从集合中删除项,则为 ;否则为 false。
例外
              millisecondsTimeout 是一个非 -1 的负数,而 -1 表示无限期超时。
该基础集合已在此 BlockingCollection<T> 实例外部进行了修改。
注解
删除项的顺序取决于用于创建实例的 BlockingCollection<T> 集合类型。 创建 BlockingCollection<T>时,可以指定要使用的集合类型。 例如,可以为先入先出 (FIFO) 行为指定对象,或者ConcurrentStack<T>为后进先出 (LIFO) 行为指定ConcurrentQueue<T>对象。 可使用实现 IProducerConsumerCollection<T> 接口的任何集合类。 BlockingCollection<T> 的默认集合类型为 ConcurrentQueue<T>。