Show / Hide Table of Contents

Class AsyncExchanger<T>

Represents a synchronization point at which two async flows can cooperate and swap elements within pairs.

Inheritance
object
Disposable
AsyncExchanger<T>
Implements
IDisposable
IAsyncDisposable
Inherited Members
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.CreateException()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(IEnumerable<IAsyncDisposable>)
Disposable.Dispose<T>(ReadOnlySpan<T>)
Disposable.DisposeAsync(params IAsyncDisposable[])
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
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 Source

AsyncExchanger()

Initializes a new asynchronous exchanger.

Declaration
public AsyncExchanger()

Properties

| Edit this page View Source

IsTerminated

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 Source

Dispose(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
Disposable.Dispose(bool)
| Edit this page View Source

DisposeAsync()

Provides graceful shutdown of this instance.

Declaration
public ValueTask DisposeAsync()
Returns
Type Description
ValueTask

The task representing state of asynchronous graceful shutdown.

| Edit this page View Source

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
Disposable.DisposeAsyncCore()
Remarks

This method makes sense only if derived class implements IAsyncDisposable interface.

| Edit this page View Source

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.

| Edit this page View Source

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

timeout is negative.

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

Implements

IDisposable
IAsyncDisposable

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾