Add Semaphoria to your services:
builder.Services.AddSemaphoria();
Inject the ISemaphoreManager into your class:
public class MyClass(ISemaphoreManager semaphoreManager)
{
public async Task MyMethod()
{
using (var semaphore = _semaphoreManager.GetSemaphore("MyKey"))
{
await semaphore.WaitAsync();
// Do something
}
}
})If you want to skip processing rather than wait you can use the IsFull() method:
var semaphore = _semaphoreManager.GetSemaphore("MyKey")
if (semaphore.IsFull())
{
// Skip processing
return;
}