Reputation: 16280
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:
async-await
, orContinueWith
Upvotes: 6
Views: 6163
Reputation: 30454
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
Upvotes: 4
Reputation: 1499790
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.
Upvotes: 16