BlockingCollection<T>.TryAdd 方法

定義

嘗試將指定的項目加入至 BlockingCollection<T>Tries to add the specified item to the BlockingCollection<T>.

多載

TryAdd(T, Int32)

嘗試在指定的時間週期內,將指定的項目加入 BlockingCollection<T>Tries to add the specified item to the BlockingCollection<T> within the specified time period.

TryAdd(T, Int32, CancellationToken)

當觀察取消語彙基元時,嘗試在指定的時間週期內,將指定的項目加入 BlockingCollection<T>Tries to add the specified item to the BlockingCollection<T> within the specified time period, while observing a cancellation token.

TryAdd(T)

嘗試將指定的項目加入至 BlockingCollection<T>Tries to add the specified item to the BlockingCollection<T>.

TryAdd(T, TimeSpan)

嘗試將指定的項目加入至 BlockingCollection<T>Tries to add the specified item to the BlockingCollection<T>.

TryAdd(T, Int32)

嘗試在指定的時間週期內,將指定的項目加入 BlockingCollection<T>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

參數

item
T

加入至集合的項目。The item to be added to the collection.

millisecondsTimeout
Int32

要等候的毫秒數,如果要無限期等候,則為 Infinite (-1)。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

傳回

如果無法在指定時間內將 item 新增至集合,則為 true;否則為 false。true if the item could be added to the collection within the specified time; otherwise, false. 如果此項目已重複,而且基礎集合不接受重複的項目,就會擲回 InvalidOperationExceptionIf the item is a duplicate, and the underlying collection does not accept duplicate items, then an InvalidOperationException is thrown.

例外狀況

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

BlockingCollection<T> 已被標記為已完成加入。The BlockingCollection<T> has been marked as complete with regards to additions.

-或--or- 基礎集合不接受這個項目。The underlying collection didn't accept the item.

另請參閱

TryAdd(T, Int32, CancellationToken)

當觀察取消語彙基元時,嘗試在指定的時間週期內,將指定的項目加入 BlockingCollection<T>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

參數

item
T

加入至集合的項目。The item to be added to the collection.

millisecondsTimeout
Int32

要等候的毫秒數,如果要無限期等候,則為 Infinite (-1)。The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
CancellationToken

要觀察的取消語彙基元。A cancellation token to observe.

傳回

如果無法在指定時間內將 item 新增至集合,則為 true;否則為 false。true if the item could be added to the collection within the specified time; otherwise, false. 如果此項目已重複,而且基礎集合不接受重複的項目,就會擲回 InvalidOperationExceptionIf the item is a duplicate, and the underlying collection does not accept duplicate items, then an InvalidOperationException is thrown.

例外狀況

BlockingCollection<T> 已處置或基礎 CancellationTokenSource 已經過處置。The BlockingCollection<T> has been disposed or the underlying CancellationTokenSource has been disposed.

millisecondsTimeout 為 -1 以外的負數,表示無限逾時。millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

BlockingCollection<T> 已被標記為已完成加入。The BlockingCollection<T> has been marked as complete with regards to additions.

-或--or- 基礎集合不接受這個項目。The underlying collection didn't accept the item.

另請參閱

TryAdd(T)

嘗試將指定的項目加入至 BlockingCollection<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

參數

item
T

加入至集合的項目。The item to be added to the collection.

傳回

若可以新增 item,則為 true;否則為 falsetrue if item could be added; otherwise, false. 如果此項目已重複,而且基礎集合不接受重複的項目,就會擲回 InvalidOperationExceptionIf the item is a duplicate, and the underlying collection does not accept duplicate items, then an InvalidOperationException is thrown.

例外狀況

BlockingCollection<T> 已被標記為已完成加入。The BlockingCollection<T> has been marked as complete with regards to additions.

-或--or- 基礎集合不接受這個項目。The underlying collection didn't accept the item.

備註

如果集合是限定集合且已滿,這個方法會立即傳回 false,而不會加入專案。If the collection is a bounded collection, and is full, this method immediately returns false without adding the item.

另請參閱

TryAdd(T, TimeSpan)

嘗試將指定的項目加入至 BlockingCollection<T>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

參數

item
T

加入至集合的項目。The item to be added to the collection.

timeout
TimeSpan

TimeSpan,代表等候毫秒數;或是 TimeSpan,代表無限期等候的 -1 毫秒。A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.

傳回

如果無法在指定時間範圍內將 item 新增至集合,則為 true;否則為 falsetrue if the item could be added to the collection within the specified time span; otherwise, false.

例外狀況

timeout 是除了 -1 毫秒以外的負數,表示無限逾時,或是大於 MaxValue 的逾時。timeout is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than MaxValue.

BlockingCollection<T> 已被標記為已完成加入。The BlockingCollection<T> has been marked as complete with regards to additions.

-或--or- 基礎集合不接受這個項目。The underlying collection didn't accept the item.

另請參閱

適用於