How to a synchronize tasks?
For an async mutual exclusion mechanism, have a look at
Building Async Coordination Primitives, Part 6: AsyncLock
You could use the AsyncLock
class follows:
private readonly AsyncLock m_lock = new AsyncLock();
async Task SaveToFileAsync()
{
using (await m_lock.LockAsync())
{
var file = await folder.GetFileAsync(...);
var stream = await file.OpenFileAsync(...);
// etc
}
}
user380719
Updated on June 15, 2022Comments
-
user380719 almost 2 years
Say I have an async method which saves to file:
async Task SaveToFileAsync() { var file = await folder.GetFileAsync ( ...) var stream = file.OpenFileAsync(...) ///etc }
Now imagine that SaveToFileAsync is called twice simultaneously. This is a problem because you can't write on the same file simultaneously
If this were a regular method, lock() would fix this:
void SaveToFile() { lock(something) { /// code here } }
However, lock is not allowed in an async method.
Of course, one could call Monitor.Enter() or use a mutex but these objects work with threads, not tasks. Therefore they are not the answer.
So since lock() is not an option, how can synchronize multiple tasks? In particular, what code should I write to ensure that "SaveToFileAsync" is called only once at a time?
-
Martin Meeser almost 8 yearsWhy is AsyncLock (and other synchronization patterns) not integral part of the TPL?
-
Christopher King over 7 yearsThe
Part 6
post doesn't give copy/paste code. Another blogger aggregated it here tho: sanjeev.dwivedi.net/?p=292