BlockingCollection<T>.TryTakeFromAny Method

Definition

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

Overloads

TryTakeFromAny(BlockingCollection<T>[], T)

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

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

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

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

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

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

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

TryTakeFromAny(BlockingCollection<T>[], T)

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item);
C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T? item);

Parameters

collections
BlockingCollection<T>[]

The array of collections.

item
T

The item that was removed from one of the collections.

Returns

The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

Exceptions

At least one of the BlockingCollection<T> instances has been disposed.

The collections argument is null.

The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

The collections argument is a 0-length array or contains a null element.

At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

Examples

The following example shows how to use the BlockingCollection<T>.TryTakeFromAny method:

C#
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);
    }
}

Remarks

A call to TryTakeFromAny may block until an item is available to be removed.

See also

Applies to

.NET 10 agus leaganacha eile
Táirge Leaganacha
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item, int millisecondsTimeout);
C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T? item, int millisecondsTimeout);

Parameters

collections
BlockingCollection<T>[]

The array of collections to remove an item from.

item
T

The item that was removed from one of the collections.

millisecondsTimeout
Int32

The number of milliseconds to wait for the item to be removed, or Infinite (-1) to wait indefinitely.

Returns

The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

Exceptions

At least one of the BlockingCollection<T> instances has been disposed.

The collections argument is null.

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

-or-

The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

The collections argument is a 0-length array or contains a null element.

At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

Remarks

A call to TryTakeFromAny may block until an item is available to be removed.

See also

Applies to

.NET 10 agus leaganacha eile
Táirge Leaganacha
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T? item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);

Parameters

collections
BlockingCollection<T>[]

The array of collections to remove an item from.

item
T

The item that was removed from one of the collections.

millisecondsTimeout
Int32

The number of milliseconds to wait for the item to be removed, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken

A cancellation token to observe.

Returns

The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

Exceptions

At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

The collections argument is null.

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

-or-

The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

The collections argument is a 0-length array or contains a null element.

At least one of the BlockingCollection<T> instances has been disposed.

Remarks

A call to TryTakeFromAny may block until an item is available to be removed.

See also

Applies to

.NET 10 agus leaganacha eile
Táirge Leaganacha
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
BlockingCollection.cs
Source:
BlockingCollection.cs
Source:
BlockingCollection.cs

Tries to remove an item from any one of the specified BlockingCollection<T> instances.

C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T item, TimeSpan timeout);
C#
public static int TryTakeFromAny(System.Collections.Concurrent.BlockingCollection<T>[] collections, out T? item, TimeSpan timeout);

Parameters

collections
BlockingCollection<T>[]

The array of collections.

item
T

The item that was removed from one of the collections.

timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

Returns

The index of the collection in the collections array from which the item was removed, or -1 if an item could not be removed.

Exceptions

At least one of the BlockingCollection<T> instances has been disposed.

The collections argument is null.

timeout is a negative number other than -1 milliseconds, which represents an infinite time-out

-or-

timeout is greater than Int32.MaxValue.

-or-

The count of collections is greater than the maximum size of 62 for STA and 63 for MTA.

The collections argument is a 0-length array or contains a null element.

At least one of the underlying collections was modified outside of its BlockingCollection<T> instance.

Remarks

A call to TryTakeFromAny may block until an item is available to be removed.

See also

Applies to

.NET 10 agus leaganacha eile
Táirge Leaganacha
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0