https://github.com/soenneker/soenneker.utils.debounce
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.
https://github.com/soenneker/soenneker.utils.debounce
csharp debounce debouncer dotnet util utility utils
Last synced: 5 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.utils.debounce
- Owner: soenneker
- License: mit
- Created: 2025-08-01T14:28:11.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2026-07-10T01:57:05.000Z (12 days ago)
- Last Synced: 2026-07-10T03:38:22.530Z (12 days ago)
- Topics: csharp, debounce, debouncer, dotnet, util, utility, utils
- Language: C#
- Homepage: https://soenneker.com
- Size: 348 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/soenneker.utils.debounce/)
[](https://github.com/soenneker/soenneker.utils.debounce/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/soenneker.utils.debounce/)
[](https://github.com/soenneker/soenneker.utils.debounce/actions/workflows/codeql.yml)
#  Soenneker.Utils.Debounce
A 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.
---
### Why would I need this?
* **API calls:** Prevent hammering a server while the user types.
* **Disk I/O:** Batch frequent save requests into a single write.
* **Telemetry:** Send aggregated metrics after bursts of activity.
* **Search boxes / auto-complete:** React only after the user pauses typing.
---
### Quick start
```bash
dotnet add package Soenneker.Utils.Debounce
```
```csharp
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();
});
}
```
#### Leading-edge execution
Pass `runLeading: true` if you want the first call to run immediately **and** the trailing call to run after the quiet period:
```csharp
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:
```csharp
await debouncer.DisposeAsync();
```
`DisposeAsync()` waits for any in-flight work to finish, ensuring graceful shutdown.
---
### Design highlights
* **Pure TPL:** Built on `System.Threading.Timer`
* **Thread-safe:** Internal state is guarded with `Interlocked` swaps.
* **Cancellation-friendly:** Each queued delegate receives *its own* `CancellationToken`.
* **Zero allocations on idle:** Work objects are created only when you call `Debounce`.
* **Tested:** xUnit suite covering timing, cancellation, and disposal semantics.