Table of Contents

Class Throttler

Namespace
MADE.Threading
Assembly
MADE.Threading.dll

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

TimeSpan

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

action Action

The action to execute.

Exceptions

ArgumentNullException

Thrown if the action is 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

action Func<Task>

The asynchronous action to execute.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

An asynchronous operation.

Exceptions

ArgumentNullException

Thrown if the action is null.

ObjectDisposedException

Thrown if the throttler has been disposed.

OperationCanceledException

Thrown if the cancellationToken is cancelled.