To lock a critical code in async fragment we should use SemaphoreSlim as in the following example
private static readonly SemaphoreSlim _syncObj = new (1,1);
...
await _syncObj.WaitAsync();
try {
...
}
finally {
_syncObj.Release();
}
Tags
C#