Class Throttler
Defines a throttler that limits execution of an action to at most once per specified time interval.
public sealed class Throttler : IDisposable
- Inheritance
-
Throttler
- Implements
- Inherited Members
- Extension Methods
Remarks
This is useful for scenarios where you want to limit the rate of execution, such as rate-limiting API calls or UI updates. Unlike Debouncer, the throttler executes the first invocation immediately and then suppresses subsequent invocations until the interval elapses.
Properties
Interval
Gets or sets the minimum interval between executions.
public TimeSpan Interval { get; set; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Throttle(Action)
Throttles the specified action. Executes immediately if the Interval has elapsed since the last execution; otherwise, the invocation is suppressed.
public void Throttle(Action action)
Parameters
actionActionThe action to execute.
Exceptions
- ArgumentNullException
Thrown if the
actionis null.- ObjectDisposedException
Thrown if the throttler has been disposed.
ThrottleAsync(Func<Task>, CancellationToken)
Throttles the specified asynchronous action. Executes immediately if the Interval has elapsed since the last execution; otherwise, the invocation is suppressed.
public Task ThrottleAsync(Func<Task> action, CancellationToken cancellationToken = default)
Parameters
actionFunc<Task>The asynchronous action to execute.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
An asynchronous operation.
Exceptions
- ArgumentNullException
Thrown if the
actionis null.- ObjectDisposedException
Thrown if the throttler has been disposed.
- OperationCanceledException
Thrown if the
cancellationTokenis cancelled.