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 use a Task below:
public async Task<bool> MutexWithAsync()
{
using (Mutex myMutex = new Mutex(false, "My mutex Name"))
{
try
{
myMutex.WaitOne();
await DoSomething();
return true;
}
catch { return false; }
finally { myMutex.ReleaseMutex(); }
}
}
If the method guarded by a Mutex is synchronous then above code will work but with async
I will get:
Object synchronization method was called from an unsynchronized block of code.
So is Named Mutex useless with asynchronous code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…