0

I found this example using async/await stuff in a book. Could please someone tell me what's the benefit of awaiting for async methods like in the example below?

using (SqlConnection connection = new SqlConnection(connectionString))
{
  SqlCommand command = new SqlCommand("SELECT * FROM People", connection);
  await connection.OpenAsync();
  SqlDataReader dataReader = **await** command.ExecuteReaderAsync();
  while (**await** dataReader.ReadAsync())
  {
    string formatStringWithMiddleName = "Person ({0}) is named {1} {2} {3}";
    string formatStringWithoutMiddleName = "Person ({0}) is named {1} {3}";
    if ((dataReader["middlename"] == null))
    {
      Console.WriteLine(formatStringWithoutMiddleName,
      dataReader["id"],
      dataReader["firstname"],
      dataReader["lastname"]);
    }
    else
    {
      Console.WriteLine(formatStringWithMiddleName,
      dataReader["id"],
      dataReader["firstname"],
      dataReader["middlename"],
      dataReader["lastname"]);
    }
  }
  dataReader.Close();
  }
}

I just can't wrap my head around this. The way I understand, await blocks until the method called (in this case, ExecuteReaderAsync and ReadAsync) returns. What's the point of calling an async method and blocking right away in the middle of the code, without actually doing anything between the call to the async method and the point when the result is returned to the caller? How is it faster or more efficient than simply doing this?

....

SqlDataReader dataReader = command.ExecuteReader();
      while (dataReader.Read())
      {
           .....
1
  • 1
    by waiting for the async method to finish, they are able to treat async code like synchronous code. This has been a problem in software development for a long time, and the way the author chose to deal with it is one of many. It's not my preferred way, but this example may also be very old. Commented Aug 3, 2016 at 17:35

2 Answers 2

5

The way I understand, await blocks until the method called (in this case, ExecuteReaderAsync and ReadAsync) returns.

No; await will pause the method and return to the caller. Thus, it does not block the calling thread for the duration of the ReadAsync operation.

Conceptually, it's the difference between synchronous and serial. Synchronous means blocking: the method call stops the thread until the Read completes. Serial means one at a time: the method will pause execution until the ReadAsync completes. So, the common await *Async() pattern is serial, but in an asynchronous way, not a synchronous way.

For more information about async and await, see my async intro post.

Sign up to request clarification or add additional context in comments.

4 Comments

Btw am I the only one who thinks that "await" contextual keyword was a bad choice? I can't think of a better variant though.
@Sergey.quixoticaxis.Ivanov: I have a blog post which is a kind of unofficial Q&A on why the async and await keywords were designed the way they were.
Interesting post, thank you. Although it mostly describes linguistic reasoning behind "await", not the philological reason why "await" was chosen instead of, for example, "leave" or "digress from" (while keeping the semantics).
Ah. Yes, there was some discussion around that, and a few variants were considered. I don't even remember what they were, now.
0

This temporarily blocks your method but keeps the UI-thread responding. Internally C# rewrites the method. The part after await is converted to a kind callback method (well not really, but you can imagine it was). The method returns immediately at the await and resumes where it has left off, when the async operation finishes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.