BlockingCollection<T>.TryAdd Method

Definition

Tries to add the specified item to the BlockingCollection<T>.

Overloads

TryAdd(T, Int32)

Tries to add the specified item to the BlockingCollection<T> within the specified time period.

TryAdd(T, Int32, CancellationToken)

Tries to add the specified item to the BlockingCollection<T> within the specified time period, while observing a cancellation token.

TryAdd(T)

Tries to add the specified item to the BlockingCollection<T>.

TryAdd(T, TimeSpan)

Tries to add the specified item to the BlockingCollection<T>.

TryAdd(T, Int32)

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

Tries to add the specified item to the BlockingCollection<T> within the specified time period.

C#
public bool TryAdd(T item, int millisecondsTimeout);

Parameters

item
T

The item to be added to the collection.

millisecondsTimeout
Int32

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

Returns

true if the item could be added to the collection within the specified time; otherwise, false. If the item is a duplicate, and the underlying collection does not accept duplicate items, then an InvalidOperationException is thrown.

Exceptions

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

The BlockingCollection<T> has been marked as complete with regards to additions.

-or-

The underlying collection didn't accept the item.

See also

Applies to

.NET 9 and other versions
Product Versions
.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
.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

TryAdd(T, Int32, CancellationToken)

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

Tries to add the specified item to the BlockingCollection<T> within the specified time period, while observing a cancellation token.

C#
public bool TryAdd(T item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);

Parameters

item
T

The item to be added to the collection.

millisecondsTimeout
Int32

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

cancellationToken
CancellationToken

A cancellation token to observe.

Returns

true if the item could be added to the collection within the specified time; otherwise, false. If the item is a duplicate, and the underlying collection does not accept duplicate items, then an InvalidOperationException is thrown.

Exceptions

The BlockingCollection<T> has been disposed or the underlying CancellationTokenSource has been disposed.

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

The BlockingCollection<T> has been marked as complete with regards to additions.

-or-

The underlying collection didn't accept the item.

See also

Applies to

.NET 9 and other versions
Product Versions
.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
.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

TryAdd(T)

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

Tries to add the specified item to the BlockingCollection<T>.

C#
public bool TryAdd(T item);

Parameters

item
T

The item to be added to the collection.

Returns

true if item could be added; otherwise, false. If the item is a duplicate, and the underlying collection does not accept duplicate items, then an InvalidOperationException is thrown.

Exceptions

The BlockingCollection<T> has been marked as complete with regards to additions.

-or-

The underlying collection didn't accept the item.

Remarks

If the collection is a bounded collection, and is full, this method immediately returns false without adding the item.

See also

Applies to

.NET 9 and other versions
Product Versions
.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
.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

TryAdd(T, TimeSpan)

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

Tries to add the specified item to the BlockingCollection<T>.

C#
public bool TryAdd(T item, TimeSpan timeout);

Parameters

item
T

The item to be added to the collection.

timeout
TimeSpan

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

Returns

true if the item could be added to the collection within the specified time span; otherwise, false.

Exceptions

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

-or-

timeout is greater than Int32.MaxValue.

The BlockingCollection<T> has been marked as complete with regards to additions.

-or-

The underlying collection didn't accept the item.

See also

Applies to

.NET 9 and other versions
Product Versions
.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
.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