How to a synchronize tasks?

19,780

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
    }
}
Share:
19,780
user380719
Author by

user380719

Updated on June 15, 2022

Comments

  • user380719
    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
    Martin Meeser almost 8 years
    Why is AsyncLock (and other synchronization patterns) not integral part of the TPL?
  • Christopher King
    Christopher King over 7 years
    The Part 6 post doesn't give copy/paste code. Another blogger aggregated it here tho: sanjeev.dwivedi.net/?p=292