Class AsyncCounter
Represents a synchronization primitive that is signaled when its count becomes non zero.
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public class AsyncCounter : QueuedSynchronizer, IAsyncEvent, ISynchronizer, IDisposable
Remarks
This class behaves in opposite to AsyncCountdownEvent. Every call of Increment() increments the counter. Every call of Wait(TimeSpan, CancellationToken) decrements counter and release the caller if the current count is greater than zero.
Constructors
| Improve this Doc View SourceAsyncCounter(Int64)
Initializes a new asynchronous counter.
Declaration
public AsyncCounter(long initialValue = 0L)
Parameters
Type | Name | Description |
---|---|---|
Int64 | initialValue | The initial value of the counter. |
Properties
| Improve this Doc View SourceValue
Gets the counter value.
Declaration
public long Value { get; }
Property Value
Type | Description |
---|---|
Int64 |
Remarks
The returned value indicates how many calls you can perform using Wait(TimeSpan, CancellationToken) without blocking.
Methods
| Improve this Doc View SourceIncrement()
Increments counter and resume suspended callers.
Declaration
public void Increment()
Wait(TimeSpan, CancellationToken)
Suspends caller if Value is zero or just decrements it.
Declaration
public Task<bool> Wait(TimeSpan timeout, CancellationToken token)
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 |
---|---|
Task<Boolean> | true if counter is decremented successfully; otherwise, false. |
Explicit Interface Implementations
| Improve this Doc View SourceIAsyncEvent.IsSet
Declaration
bool IAsyncEvent.IsSet { get; }
Returns
Type | Description |
---|---|
Boolean |
IAsyncEvent.Reset()
Declaration
bool IAsyncEvent.Reset()
Returns
Type | Description |
---|---|
Boolean |
IAsyncEvent.Signal()
Declaration
bool IAsyncEvent.Signal()
Returns
Type | Description |
---|---|
Boolean |