6

I have the following method which commits changes to a db (using Entity Framework):

public async Task<int> CommitAsync(Info info)
{
    if (this.Database.Connection.State == ConnectionState.Closed)
        await this.Database.Connection.OpenAsync();

    await SetInfo(info);
    return await base.SaveChangesAsync();
}

Is the above method safe to use as is, or should I:

  1. Avoid using async-await, or
  2. Use ContinueWith
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263

2 Answers2

16

It's absolutely fine to have multiple await expressions in the same async method - it would be relatively useless feature otherwise.

Basically, the method will execute synchronously until it reaches the first await where the awaitable involved hasn't already completed. It will then return to the caller, having set up a continuation for the awaitable to execute the rest of the async method. If execution later reaches another await expression where the awaitable hasn't already completed, a continuation is set up on that awaitable, etc.

Each time the method "resumes" from an await, it carries on where it left off, with the same local variables etc. This is achieved by the compiler building a state machine on your behalf.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Is my understanding correct? If we have two awaits the code after the second await would only be continuation for the second await not for the first await If it wasn't the case then code after the second await would have run twice (first, as a continuation to the first awaitable and second as a continuation to the second awaitable. – OldSchool Aug 13 '17 at 10:13
  • @Rouftantical: Yes, as I said "it carries on from where it left off". – Jon Skeet Aug 13 '17 at 11:19
4

Your code looks perfect. It gives your caller the opportunity to do something useful at moments you are waiting instead of everyone doing a busy wait until everything is finished.

The nice thing of using async-await instead of using ContinueWith is that your code looks fairly synchronous. It is easy to see in which order the statements will be executed. ContinueWith also lets you specify the order, but it is a bit more difficult to see.

If a thread enters an async procedure it executes the procedure until it meets an await. Instead of waiting until the awaited procedure is finished, control is given back to your caller who can continue performing the next statements until he meets an await, where control is given to his caller etc. Once everyone is awaiting and your OpenAsync is finished the thread continues doing the statements after OpenAsync until it meets another await.

Someone here in stackoverflow (alas lost his name) explained me once the async-await process in a breakfast metaphor.

A very useful introduction, Stephen Cleary about Async-await. Lets you also understand how async-await prevents problems with InvokeRequired

Community
  • 1
  • 1
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • 2
    I think the breakfast metaphor was started with Eric Lippert. Here's a link to some parts of it. http://www.i-programmer.info/professional-programmer/i-programmer/7154-c-guru-an-interview-with-eric-lippert.html?start=1 I've been following both Skeet, Cleary and Lippert like a hawk in the past few years or so. Be sure to read up on bowl of fruit and animal cages metaphor too. – Zuzlx May 02 '16 at 17:39