Show / Hide Table of Contents

Class AsyncCounter

Represents a synchronization primitive that is signaled when its count becomes non-zero.

Inheritance
object
Disposable
QueuedSynchronizer
AsyncCounter
Implements
IAsyncEvent
IDisposable
IResettable
Inherited Members
QueuedSynchronizer.MeasurementTags
QueuedSynchronizer.TrackSuspendedCallers(Func<object>)
QueuedSynchronizer.SetCallerInformation(object)
QueuedSynchronizer.GetSuspendedCallers()
QueuedSynchronizer.CancelSuspendedCallers(CancellationToken)
QueuedSynchronizer.Dispose(bool)
QueuedSynchronizer.Dispose(Exception)
QueuedSynchronizer.DisposeAsyncCore()
QueuedSynchronizer.DisposeAsync()
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 AsyncCounter : QueuedSynchronizer, IAsyncEvent, IDisposable, IResettable
Remarks

This class behaves in opposite to AsyncCountdownEvent. Every call of Increment() increments the counter. Every call of WaitAsync(TimeSpan, CancellationToken) decrements counter and release the caller if the current count is greater than zero.

Constructors

| Edit this page View Source

AsyncCounter(long)

Initializes a new asynchronous counter.

Declaration
public AsyncCounter(long initialValue = 0)
Parameters
Type Name Description
long initialValue

The initial value of the counter.

Exceptions
Type Condition
ArgumentOutOfRangeException

initialValue is less than zero.

| Edit this page View Source

AsyncCounter(long, int)

Initializes a new asynchronous counter.

Declaration
public AsyncCounter(long initialValue, int concurrencyLevel)
Parameters
Type Name Description
long initialValue

The initial value of the counter.

int concurrencyLevel

The expected number of concurrent flows.

Exceptions
Type Condition
ArgumentOutOfRangeException

concurrencyLevel is less than or equal to zero.

Properties

| Edit this page View Source

Value

Gets the counter value.

Declaration
public long Value { get; }
Property Value
Type Description
long
Remarks

The returned value indicates how many calls you can perform using WaitAsync(TimeSpan, CancellationToken) without blocking.

Methods

| Edit this page View Source

Increment()

Increments counter and resume suspended callers.

Declaration
public void Increment()
Exceptions
Type Condition
OverflowException

Counter overflow detected.

ObjectDisposedException

This object is disposed.

| Edit this page View Source

Increment(long)

Increments counter and resume suspended callers.

Declaration
public void Increment(long delta)
Parameters
Type Name Description
long delta

The value to be added to the counter.

Exceptions
Type Condition
ArgumentOutOfRangeException

delta is less than zero.

ObjectDisposedException

This object is disposed.

OverflowException

Counter overflow detected.

| Edit this page View Source

TryDecrement()

Attempts to decrement the counter synchronously.

Declaration
public bool TryDecrement()
Returns
Type Description
bool

true if the counter decremented successfully; false if this counter is already zero.

| Edit this page View Source

TryIncrement(long)

Attempts to increment this counter.

Declaration
public bool TryIncrement(long maxValue)
Parameters
Type Name Description
long maxValue

The maximum allowed value of this counter.

Returns
Type Description
bool

true if successfully incremented; false on overflow.

Exceptions
Type Condition
ArgumentOutOfRangeException

maxValue is negative or zero.

| Edit this page View Source

WaitAsync(CancellationToken)

Suspends caller if Value is zero or just decrements it.

Declaration
public ValueTask WaitAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

The token that can be used to cancel the waiting operation.

Returns
Type Description
ValueTask

The task representing asynchronous result.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

ObjectDisposedException

This object is disposed.

| Edit this page View Source

WaitAsync(TimeSpan, CancellationToken)

Suspends caller if Value is zero or just decrements it.

Declaration
public ValueTask<bool> WaitAsync(TimeSpan timeout, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan timeout

Time to wait for increment.

CancellationToken token

The token that can be used to cancel the waiting operation.

Returns
Type Description
ValueTask<bool>

true if counter is decremented successfully; otherwise, false.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

ObjectDisposedException

This object is disposed.

Implements

IAsyncEvent
IDisposable
IResettable

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
☀
☾