Solution: Using the ‘await’ operator with a lock statement

You won’t be able to lock a resource with async usage, using the lock keyword, due to possible deadlocks that can occur.

The following code will allow you to lock a resource for ASYNC usage:

//Use SemaphoreSlim.WaitAsync method.
System.Threading.SemaphoreSlim mySemaphoreSlim = new SemaphoreSlim(1);

public async Task NextTick()
	await mySemaphoreSlim.WaitAsync();
	    await SomeAsyncTaskHereUsingaResource();


What's your thoughts on this?


Protected by WP Anti Spam