Class AsyncCounter
Represents a synchronization primitive that is signaled when its count becomes non-zero.
Inherited Members
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 SourceAsyncCounter(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 |
|
Properties
| Edit this page View SourceValue
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 SourceIncrement()
Increments counter and resume suspended callers.
Declaration
public void Increment()
Exceptions
| Type | Condition |
|---|---|
| OverflowException | Counter overflow detected. |
| ObjectDisposedException | This object is disposed. |
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 |
|
| ObjectDisposedException | This object is disposed. |
| OverflowException | Counter overflow detected. |
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. |
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 |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
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. |
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. |