Edit

Share via


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.

public:
 bool TryAdd(T item, int millisecondsTimeout);
public bool TryAdd (T item, int millisecondsTimeout);
member this.TryAdd : 'T * int -> bool
Public Function TryAdd (item As T, millisecondsTimeout As Integer) As Boolean

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

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.

public:
 bool TryAdd(T item, int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public bool TryAdd (T item, int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
member this.TryAdd : 'T * int * System.Threading.CancellationToken -> bool
Public Function TryAdd (item As T, millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Boolean

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

TryAdd(T)

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

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

public:
 bool TryAdd(T item);
public bool TryAdd (T item);
member this.TryAdd : 'T -> bool
Public Function TryAdd (item As T) As Boolean

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

TryAdd(T, TimeSpan)

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

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

public:
 bool TryAdd(T item, TimeSpan timeout);
public bool TryAdd (T item, TimeSpan timeout);
member this.TryAdd : 'T * TimeSpan -> bool
Public Function TryAdd (item As T, timeout As TimeSpan) As Boolean

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