Skip to content

Instantly share code, notes, and snippets.

@marcominerva
Last active April 17, 2020 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcominerva/04629cb9e95ad1f29b42ea0ec9d690bb to your computer and use it in GitHub Desktop.
Save marcominerva/04629cb9e95ad1f29b42ea0ec9d690bb to your computer and use it in GitHub Desktop.
How to implement a simple lock in an asynchronous workflow
using System.Threading.Tasks;
namespace System.Threading
{
public class AsyncLock : IDisposable
{
private readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);
public async Task<AsyncLock> LockAsync()
{
await semaphoreSlim.WaitAsync();
return this;
}
public void Dispose()
{
semaphoreSlim.Release();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment