Last active
April 17, 2020 13:49
-
-
Save marcominerva/04629cb9e95ad1f29b42ea0ec9d690bb to your computer and use it in GitHub Desktop.
How to implement a simple lock in an asynchronous workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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