using System; using System.Threading; using System.Threading.Tasks; //https://stackoverflow.com/questions/7612602/why-cant-i-use-the-await-operator-within-the-body-of-a-lock-statement/50139704#50139704 public class SemaphoreLocker { private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); public async Task LockAsync(Func worker) { await _semaphore.WaitAsync(); try { await worker(); } finally { _semaphore.Release(); } } // overloading variant for non-void methods with return type (generic T) public async Task LockAsync(Func> worker) { await _semaphore.WaitAsync(); try { return await worker(); } finally { _semaphore.Release(); } } }