Class AsyncExchanger<T>
Represents a synchronization point at which two async flows can cooperate and swap elements within pairs.
Implements
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncExchanger<T> : Disposable, IAsyncDisposable
Type Parameters
Name | Description |
---|---|
T | The type of objects that may be exchanged. |
Remarks
This type is useful to organize pipeline between consumer and producer.
Properties
| Improve this Doc View SourceIsTerminated
Gets a value indicating whether this exchange has been terminated.
Declaration
public bool IsTerminated { get; }
Property Value
Type | Description |
---|---|
Boolean |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | The object has been disposed. |
Methods
| Improve this Doc View SourceDispose(Boolean)
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
Boolean | disposing |
DisposeAsync()
Provides graceful shutdown of this instance.
Declaration
public ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
ValueTask | The task representing state of asynchronous graceful shutdown. |
ExchangeAsync(T, CancellationToken)
Waits for another flow to arrive at this exchange point, then transfers the given object to it, receiving its object as return value.
Declaration
public ValueTask<T> ExchangeAsync(T value, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
T | value | The object to exchange. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask<T> | The object provided by another async flow. |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | The operation has been canceled. |
ObjectDisposedException | The object has been disposed. |
ExchangeTerminatedException | The exhange has been terminated. |
ExchangeAsync(T, TimeSpan, CancellationToken)
Waits for another flow to arrive at this exchange point, then transfers the given object to it, receiving its object as return value.
Declaration
public ValueTask<T> ExchangeAsync(T value, TimeSpan timeout, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
T | value | The object to exchange. |
TimeSpan | timeout | The maximum time to wait. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
ValueTask<T> | The object provided by another async flow. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
OperationCanceledException | The operation has been canceled or timed out. |
ObjectDisposedException | The object has been disposed. |
ExchangeTerminatedException | The exhange has been terminated. |
Terminate(Exception)
Informs another participant that no more data will be exchanged with it.
Declaration
public void Terminate(Exception exception = null)
Parameters
Type | Name | Description |
---|---|---|
Exception | exception | The optional exception indicating termination reason. |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | The object has been disposed. |
InvalidOperationException | The exchange is already terminated. |
TryExchange(ref T)
Attempts to transfer the object to another flow synchronously.
Declaration
public bool TryExchange(ref T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The object to exchange. |
Returns
Type | Description |
---|---|
Boolean | true if another flow is ready for exchange; otherwise, false. |
Remarks
value
remains unchanged if return value is false.
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | The object has been disposed. |
ExchangeTerminatedException | The exhange has been terminated. |