Class AsyncExchanger<T>
Represents a synchronization point at which two async flows can cooperate and swap elements within pairs.
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncExchanger<T> : Disposable, IDisposable, 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.
Constructors
| Edit this page View SourceAsyncExchanger()
Initializes a new asynchronous exchanger.
Declaration
public AsyncExchanger()
Properties
| Edit this page View SourceIsTerminated
Gets a value indicating whether this exchange has been terminated.
Declaration
public bool IsTerminated { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | The object has been disposed. |
Methods
| Edit this page View SourceDispose(bool)
Releases managed and unmanaged resources associated with this object.
Declaration
protected override void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | true if called from Dispose(); false if called from finalizer ~Disposable(). |
Overrides
| Edit this page View SourceDisposeAsync()
Provides graceful shutdown of this instance.
Declaration
public ValueTask DisposeAsync()
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing state of asynchronous graceful shutdown. |
DisposeAsyncCore()
Releases managed resources associated with this object asynchronously.
Declaration
protected override ValueTask DisposeAsyncCore()
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing asynchronous execution of this method. |
Overrides
Remarks
This method makes sense only if derived class implements IAsyncDisposable interface.
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)
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 exchange 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)
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 |
|
| TimeoutException | The operation has timed out. |
| OperationCanceledException | The operation has been canceled or timed out. |
| ObjectDisposedException | The object has been disposed. |
| ExchangeTerminatedException | The exchange 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 |
|---|---|
| bool | 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 exchange has been terminated. |