30 lines
622 B
C#
30 lines
622 B
C#
namespace BlueMine.Redmine
|
|
{
|
|
public class AsyncLock
|
|
{
|
|
private readonly SemaphoreSlim _semaphore = new(1, 1);
|
|
|
|
public async Task<IDisposable> LockAsync()
|
|
{
|
|
await _semaphore.WaitAsync();
|
|
return new Disposable(() => _semaphore.Release());
|
|
}
|
|
|
|
private class Disposable : IDisposable
|
|
{
|
|
private readonly Action _action;
|
|
|
|
public Disposable(Action action)
|
|
{
|
|
_action = action;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_action();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|