user380719
user380719

Reputation: 9913

How to a synchronize tasks?

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?

Upvotes: 22

Views: 23453

Answers (1)

dtb
dtb

Reputation: 217361

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
    }
}

Upvotes: 27

Related Questions