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, ISynchronizer, IDisposable
  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
| 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.  | 
      
Exceptions
| Type | Condition | 
|---|---|
| ArgumentOutOfRangeException | 
  | 
      
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 WaitAsync(TimeSpan, CancellationToken) without blocking.
Methods
| Improve this Doc View SourceIncrement()
Increments counter and resume suspended callers.
Declaration
public void Increment()
  Exceptions
| Type | Condition | 
|---|---|
| ObjectDisposedException | This object is disposed.  | 
      
WaitAsync(TimeSpan, CancellationToken)
Suspends caller if Value is zero or just decrements it.
Declaration
public Task<bool> WaitAsync(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.  | 
      
Exceptions
| Type | Condition | 
|---|---|
| OperationCanceledException | The operation has been canceled.  | 
      
| ObjectDisposedException | This object is disposed.  | 
      
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 |