BlockingCollection<T>.TryAdd BlockingCollection<T>.TryAdd BlockingCollection<T>.TryAdd BlockingCollection<T>.TryAdd Method

Definition

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

Overloads

TryAdd(T, Int32) TryAdd(T, Int32) TryAdd(T, Int32) TryAdd(T, Int32)

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

TryAdd(T, Int32, CancellationToken) TryAdd(T, Int32, CancellationToken) 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) TryAdd(T) TryAdd(T) TryAdd(T)

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

TryAdd(T, TimeSpan) TryAdd(T, TimeSpan) TryAdd(T, TimeSpan) TryAdd(T, TimeSpan)

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

TryAdd(T, Int32) TryAdd(T, Int32) TryAdd(T, Int32) TryAdd(T, Int32)

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 T T T

The item to be added to the collection.

millisecondsTimeout
Int32 Int32 Int32 Int32

The number of milliseconds to wait, 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

TryAdd(T, Int32, CancellationToken) TryAdd(T, Int32, CancellationToken) TryAdd(T, Int32, CancellationToken)

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

Parameters

item
T T T T

The item to be added to the collection.

millisecondsTimeout
Int32 Int32 Int32 Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken CancellationToken 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

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

TryAdd(T) TryAdd(T) TryAdd(T) TryAdd(T)

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 T T 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

TryAdd(T, TimeSpan) TryAdd(T, TimeSpan) TryAdd(T, TimeSpan) TryAdd(T, TimeSpan)

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 T T T

The item to be added to the collection.

timeout
TimeSpan TimeSpan TimeSpan 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 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