Hence I can\'t use thread-affine locks with async - how can I guard my resources when running multiple processes?
For example I\'ve two processes that u
You can use a binary Semaphore instead of a Mutex. A Semaphore does not need to be release by the same thread that acquired it. The big disadvantage here is if the application crashes or is killed within DoSomething() the semaphore will not be released and the next instance of the app will hang. See Abandoned named semaphore not released
public async Task MutexWithAsync()
{
using (Semaphore semaphore = new Semaphore(1, 1, "My semaphore Name"))
{
try
{
semaphore.WaitOne();
await DoSomething();
return true;
}
catch { return false; }
finally { semaphore.Release(); }
}
}