語言

BlockingCollection<T>.TryTakeFromAny 方法

定義

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

多載

名稱 Description
TryTakeFromAny(BlockingCollection<T>[], T)

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

TryTakeFromAny(BlockingCollection<T>[], T)

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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);
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>[]

收藏的陣列。

item
T

那個從其中一個收藏中被移除的項目。

傳回

該項目被移除的陣列中 collections 集合的索引,或若無法移除項目則 -1。

例外狀況

至少有一個 BlockingCollection<T> 個案已被處理。

這個 collections 論證是空的。

這個 collections 數量超過STA的最大62和MTA的最大大小63。

參數 collections 為 0 長度的陣列,或包含 null 元素。

至少有一個底層集合在其實例外 BlockingCollection<T> 被修改。

範例

下列範例示範如何使用 BlockingCollection<T>.TryTakeFromAny 方法:

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);
    }
}
module FromToAnyDemo =
    // Demonstrates:
    //      Bounded BlockingCollection<T>
    //      BlockingCollection<T>.TryAddToAny()
    //      BlockingCollection<T>.TryTakeFromAny()
    let blockingCollectionFromToAny () =
        let bcs = 
            [|
                new BlockingCollection<int>(5) // collection bounded to 5 items
                new BlockingCollection<int>(5) // collection bounded to 5 items
             |]
        // Should be able to add 10 items w/o blocking
        let mutable numFailures = 0;
        for i = 0 to 9 do
            if BlockingCollection<int>.TryAddToAny(bcs, i) = -1 then
                numFailures <- numFailures + 1
        printfn $"TryAddToAny: {numFailures} failures (should be 0)"

        // Should be able to retrieve 10 items
        let mutable numItems = 0
        let mutable item = 0
        while BlockingCollection<int>.TryTakeFromAny(bcs, &item) <> -1 do
            numItems <- numItems + 1
        printfn $"TryTakeFromAny: retrieved {numItems} items (should be 10)"
'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

備註

致電 TryTakeFromAny 可能會封鎖,直到有物品可供移除。

另請參閱

適用於

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

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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);
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>[]

要移除項目的集合陣列。

item
T

那個從其中一個收藏中被移除的項目。

millisecondsTimeout
Int32

等待物品移除的時間,或 Infinite 無限等待的時間(-1)。

傳回

該項目被移除的陣列中 collections 集合的索引,或若無法移除項目則 -1。

例外狀況

至少有一個 BlockingCollection<T> 個案已被處理。

這個 collections 論證是空的。

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

-或-

這個 collections 數量超過STA的最大62和MTA的最大大小63。

參數 collections 為 0 長度的陣列,或包含 null 元素。

至少有一個底層集合在其實例外 BlockingCollection<T> 被修改。

備註

致電 TryTakeFromAny 可能會封鎖,直到有物品可供移除。

另請參閱

適用於

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

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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);
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
Public Shared Function TryTakeFromAny (collections As BlockingCollection(Of T)(), ByRef item As T, millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Integer

參數

collections
BlockingCollection<T>[]

要移除項目的集合陣列。

item
T

那個從其中一個收藏中被移除的項目。

millisecondsTimeout
Int32

等待物品移除的時間,或 Infinite 無限等待的時間(-1)。

cancellationToken
CancellationToken

一個可以觀察的取消標記。

傳回

該項目被移除的陣列中 collections 集合的索引,或若無法移除項目則 -1。

例外狀況

至少有一個底層集合在其實例外 BlockingCollection<T> 被修改。

這個 collections 論證是空的。

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

-或-

這個 collections 數量超過STA的最大62和MTA的最大大小63。

參數 collections 為 0 長度的陣列,或包含 null 元素。

至少有一個 BlockingCollection<T> 個案已被處理。

備註

致電 TryTakeFromAny 可能會封鎖,直到有物品可供移除。

另請參閱

適用於

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

來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs
來源:
BlockingCollection.cs

嘗試從任一指定 BlockingCollection<T> 實例中移除項目。

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);
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>[]

收藏的陣列。

item
T

那個從其中一個收藏中被移除的項目。

timeout
TimeSpan

TimeSpan A 代表等待的毫秒數,或 A TimeSpan 代表無限等待的 -1 毫秒。

傳回

該項目被移除的陣列中 collections 集合的索引,或若無法移除項目則 -1。

例外狀況

至少有一個 BlockingCollection<T> 個案已被處理。

這個 collections 論證是空的。

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

-或-

timeout 大於 Int32.MaxValue。

-或-

這個 collections 數量超過STA的最大62和MTA的最大大小63。

參數 collections 為 0 長度的陣列,或包含 null 元素。

至少有一個底層集合在其實例外 BlockingCollection<T> 被修改。

備註

致電 TryTakeFromAny 可能會封鎖,直到有物品可供移除。

另請參閱

適用於