Method Timeout
Timeout<TSource>(IAsyncEnumerable<TSource>, TimeSpan)
Applies a timeout policy for each element in the async-enumerable sequence. If the next element isn't received within the specified timeout duration, a TimeoutException is propagated to the consumer.
Declaration
public static IAsyncEnumerable<TSource> Timeout<TSource>(this IAsyncEnumerable<TSource> source, TimeSpan timeout)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncEnumerable<TSource> | source | Source sequence to perform a timeout for. |
| TimeSpan | timeout | Maximum duration between values before a timeout occurs. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<TSource> | The source sequence with a TimeoutException in case of a timeout. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements in the source sequence. |
Remarks
Specifying a Zero value for timeout is not recommended but
supported, causing timeout timers to be scheduled that are due immediately. However, this doesn't guarantee
a timeout will occur. If the iteration is synchronous, then the timeout will not be evaluated at all.
Additionally, even a Zero timeout has a minimum time to be handled, and the action to
propagate a timeout may not execute immediately. In such cases, the next element may arrive before the
scheduler gets a chance to run the timeout action.
Note: If source is completely synchronous, then the timeout
will not be evaluated at all. If the iteration is synchronous and takes longer than timeout, no exception will be thrown.
This operator does not throw immediately on the expiration of timeout. It is a violation
of spec to attempt to dispose or otherwise interact with an IAsyncEnumerator<T> while the MoveNextAsync() task is not completed. The CancellationToken
provided to the inner enumerable will be canceled when the timeout is expired, but this
operator will continue to wait until the task is complete or canceled before throwing a TimeoutException.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
| TimeoutException | If no element is produced within |