BlockingCollection<T>.TryTakeFromAny 方法

定義

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

多載

TryTakeFromAny(BlockingCollection<T>[], T)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

TryTakeFromAny(BlockingCollection<T>[], T, Int32)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

TryTakeFromAny(BlockingCollection<T>[], T, Int32, CancellationToken)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

TryTakeFromAny(BlockingCollection<T>[], T, TimeSpan)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

TryTakeFromAny(BlockingCollection<T>[], T)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

public:
 static int TryTakeFromAny(cli::array <System::Collections::Concurrent::BlockingCollection<T> ^> ^ collections, [Runtime::InteropServices::Out] T % item);
public static int TryTakeFromAny (System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item);
static member TryTakeFromAny : System.Collections.Concurrent.BlockingCollection<'T>[] * 'T -> int
Public Shared Function TryTakeFromAny (collections As BlockingCollection(Of T)(), ByRef item As T) As Integer

參數

collections
BlockingCollection<T>[]

集合的陣列。The array of collections.

item
T

從其中一個集合移除的項目。The item removed from one of the collections.

傳回

所移除之項目所在的 collections 陣列中集合的索引,如果無法移除項目則為 -1。The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

例外狀況

至少其中一個 BlockingCollection<T> 執行個體已經處置。At least one of the BlockingCollection<T> instances has been disposed.

collections 引數為 null。The collections argument is null.

collections 的計數大於 STA 的上限 62 和 MTA 的上限 63。The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

collections 引數是長度為 0 的陣列,或包含 Null 項目。The collections argument is a 0-length array or contains a null element.

至少其中一個基礎集合已在其 BlockingCollection<T> 執行個體外修改。At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

範例

下列範例顯示如何使用 BlockingCollection<T>.TryTakeFromAny 方法:The following example shows how to use the BlockingCollection<T>.TryTakeFromAny method:

class FromToAnyDemo
{
    // Demonstrates:
    //      Bounded BlockingCollection<T>
    //      BlockingCollection<T>.TryAddToAny()
    //      BlockingCollection<T>.TryTakeFromAny()
    public static void BC_FromToAny()
    {
        BlockingCollection<int>[] bcs = new BlockingCollection<int>[2];
        bcs[0] = new BlockingCollection<int>(5); // collection bounded to 5 items
        bcs[1] = new BlockingCollection<int>(5); // collection bounded to 5 items

        // Should be able to add 10 items w/o blocking
        int numFailures = 0;
        for (int i = 0; i < 10; i++)
        {
            if (BlockingCollection<int>.TryAddToAny(bcs, i) == -1) numFailures++;
        }
        Console.WriteLine("TryAddToAny: {0} failures (should be 0)", numFailures);

        // Should be able to retrieve 10 items
        int numItems = 0;
        int item;
        while (BlockingCollection<int>.TryTakeFromAny(bcs, out item) != -1) numItems++;
        Console.WriteLine("TryTakeFromAny: retrieved {0} items (should be 10)", numItems);
    }
}
'Imports System.Threading.Tasks
'Imports System.Collections.Concurrent

' Demonstrates:
' Bounded BlockingCollection<T>
' BlockingCollection<T>.TryAddToAny()
' BlockingCollection<T>.TryTakeFromAny()
Class ToAnyDemo
    Shared Sub BC_ToAny()
        Dim bcs As BlockingCollection(Of Integer)() = New BlockingCollection(Of Integer)(1) {}
        bcs(0) = New BlockingCollection(Of Integer)(5)
        ' collection bounded to 5 items
        bcs(1) = New BlockingCollection(Of Integer)(5)
        ' collection bounded to 5 items
        ' Should be able to add 10 items w/o blocking
        Dim numFailures As Integer = 0
        For i As Integer = 0 To 9
            If BlockingCollection(Of Integer).TryAddToAny(bcs, i) = -1 Then
                numFailures += 1
            End If
        Next
        Console.WriteLine("TryAddToAny: {0} failures (should be 0)", numFailures)

        ' Should be able to retrieve 10 items
        Dim numItems As Integer = 0
        Dim item As Integer
        While BlockingCollection(Of Integer).TryTakeFromAny(bcs, item) <> -1
            numItems += 1
        End While
        Console.WriteLine("TryTakeFromAny: retrieved {0} items (should be 10)", numItems)
    End Sub
End Class

備註

呼叫 T>.trytakefromany 可能會封鎖,直到可以移除專案為止。A call to TryTakeFromAny may block until an item is available to be removed.

另請參閱

TryTakeFromAny(BlockingCollection<T>[], T, Int32)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

public:
 static int TryTakeFromAny(cli::array <System::Collections::Concurrent::BlockingCollection<T> ^> ^ collections, [Runtime::InteropServices::Out] T % item, int millisecondsTimeout);
public static int TryTakeFromAny (System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item, int millisecondsTimeout);
static member TryTakeFromAny : System.Collections.Concurrent.BlockingCollection<'T>[] * 'T * int -> int
Public Shared Function TryTakeFromAny (collections As BlockingCollection(Of T)(), ByRef item As T, millisecondsTimeout As Integer) As Integer

參數

collections
BlockingCollection<T>[]

集合的陣列。The array of collections.

item
T

從其中一個集合移除的項目。The item removed from one of the collections.

millisecondsTimeout
Int32

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

傳回

所移除之項目所在的 collections 陣列中集合的索引,如果無法移除項目則為 -1。The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

例外狀況

至少其中一個 BlockingCollection<T> 執行個體已經處置。At least one of the BlockingCollection<T> instances has been disposed.

collections 引數為 null。The collections argument is null.

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out. -或--or- collections 的計數大於 STA 的上限 62 和 MTA 的上限 63。The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

collections 引數是長度為 0 的陣列,或包含 Null 項目。The collections argument is a 0-length array or contains a null element.

至少其中一個基礎集合已在其 BlockingCollection<T> 執行個體外修改。At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

備註

呼叫 T>.trytakefromany 可能會封鎖,直到可以移除專案為止。A call to TryTakeFromAny may block until an item is available to be removed.

另請參閱

TryTakeFromAny(BlockingCollection<T>[], T, Int32, CancellationToken)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

public:
 static int TryTakeFromAny(cli::array <System::Collections::Concurrent::BlockingCollection<T> ^> ^ collections, [Runtime::InteropServices::Out] T % item, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public static int TryTakeFromAny (System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
static member TryTakeFromAny : System.Collections.Concurrent.BlockingCollection<'T>[] * 'T * int * System.Threading.CancellationToken -> int

參數

collections
BlockingCollection<T>[]

集合的陣列。The array of collections.

item
T

從其中一個集合移除的項目。The item removed from one of the collections.

millisecondsTimeout
Int32

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

cancellationToken
CancellationToken

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

傳回

所移除之項目所在的 collections 陣列中集合的索引,如果無法移除項目則為 -1。The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

例外狀況

至少其中一個基礎集合已在其 BlockingCollection<T> 執行個體外修改。At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

collections 引數為 null。The collections argument is null.

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out. -或--or- collections 的計數大於 STA 的上限 62 和 MTA 的上限 63。The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

collections 引數是長度為 0 的陣列,或包含 Null 項目。The collections argument is a 0-length array or contains a null element.

至少其中一個 BlockingCollection<T> 執行個體已經處置。At least one of the BlockingCollection<T> instances has been disposed.

備註

呼叫 T>.trytakefromany 可能會封鎖,直到可以移除專案為止。A call to TryTakeFromAny may block until an item is available to be removed.

另請參閱

TryTakeFromAny(BlockingCollection<T>[], T, TimeSpan)

嘗試從任何一個指定的 BlockingCollection<T> 執行個體移除項目。Tries to remove an item from any one of the specified BlockingCollection<T> instances.

public:
 static int TryTakeFromAny(cli::array <System::Collections::Concurrent::BlockingCollection<T> ^> ^ collections, [Runtime::InteropServices::Out] T % item, TimeSpan timeout);
public static int TryTakeFromAny (System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item, TimeSpan timeout);
static member TryTakeFromAny : System.Collections.Concurrent.BlockingCollection<'T>[] * 'T * TimeSpan -> int
Public Shared Function TryTakeFromAny (collections As BlockingCollection(Of T)(), ByRef item As T, timeout As TimeSpan) As Integer

參數

collections
BlockingCollection<T>[]

集合的陣列。The array of collections.

item
T

從其中一個集合移除的項目。The item removed from one of the collections.

timeout
TimeSpan

TimeSpan,代表等候毫秒數;或是 TimeSpan,代表無限期等候的 -1 毫秒。A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

傳回

所移除之項目所在的 collections 陣列中集合的索引,如果無法移除項目則為 -1。The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

例外狀況

至少其中一個 BlockingCollection<T> 執行個體已經處置。At least one of the BlockingCollection<T> instances has been disposed.

collections 引數為 null。The collections argument is null.

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

-或--or- collections 的計數大於 STA 的上限 62 和 MTA 的上限 63。The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

collections 引數是長度為 0 的陣列,或包含 Null 項目。The collections argument is a 0-length array or contains a null element.

至少其中一個基礎集合已在其 BlockingCollection<T> 執行個體外修改。At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

備註

呼叫 T>.trytakefromany 可能會封鎖,直到可以移除專案為止。A call to TryTakeFromAny may block until an item is available to be removed.

另請參閱

適用於