Delays execution of an async action until a specified interval has passed without new invocations. Ideal for throttling high-frequency operations like UI events, logging, or API calls.
$ dotnet add package Soenneker.Utils.Debounce
Soenneker.Utils.DebounceA utility that lets you debounce work in .NET.
Give it a delay, async/sync delegate, and the Debouncer guarantees that multiple rapid calls collapse into exactly one invocation.
dotnet add package Soenneker.Utils.Debounce
using Soenneker.Utils.Debounce;
var debouncer = new Debouncer();
// Fire only once, 300 ms after the *last* request:
void OnTextChanged(string text)
{
debouncer.Debounce(
delayMs: 300,
action: async ct =>
{
var results = await SearchAsync(text, ct);
UpdateUI(results);
});
}
void OnResize()
{
debouncer.Debounce(
delayMs: 250,
action: () =>
{
// Runs on the thread-pool after 250 ms of quiescence
SaveWindowLayout();
});
}
Pass runLeading: true if you want the first call to run immediately and the trailing call to run after the quiet period:
debouncer.Debounce(
delayMs: 500,
runLeading: true,
action: ct => Logger.LogAsync("Burst started", ct));
Either wrap it in a using statement or dispose the debouncer when you’re done:
await debouncer.DisposeAsync();
DisposeAsync() waits for any in-flight work to finish, ensuring graceful shutdown.
System.Threading.TimerInterlocked swaps.CancellationToken.Debounce.