2

I'm confused about C# async. I understand async for parallel task processing. For example, a method does A and B task, and async enables A and B do action in the same time; B doesn't have to wait until A be done.

But the below example code does only a single task which is pulling data from a database. It means, there is nothing for parallel tasks. But why does it use async?

Please give me an advice so that I can understand async.

[ResponseType(typeof(BookDetailDTO))]
public async Task<IHttpActionResult> GetBook(int id)
{
    var book = await db.Books.Include(b => b.Author).Select(b =>
        new BookDetailDTO()
        {
            Id = b.Id,
            Title = b.Title,
            Year = b.Year,
            Price = b.Price,
            AuthorName = b.Author.Name,
            Genre = b.Genre
        }).SingleOrDefaultAsync(b => b.Id == id);
    if (book == null)
    {
        return NotFound();
    }

    return Ok(book);
}
3
  • 1
    msdn.microsoft.com/en-us/library/mt674882.aspx Commented Jun 19, 2016 at 12:24
  • it's the best article / explanation, especially 'Threads' section - it's clarify what you misunderstood. Commented Jun 19, 2016 at 12:36
  • 1
    Just for the record "async enables A and B do action in the same time;" is not correct. In most cases it's not at the same time. A better way to put is to think about it as "async/awaits is a synctactic sugar which makes it easy to make the program do A while it waits for the results of B without writing complicated code" Commented Dec 22, 2016 at 0:32

3 Answers 3

6

I explain this in detail in my async ASP.NET article. In summary, async works by freeing up threads, so those threads can be used for other things.

means, there is nothing for parallel tasks. But why does it use async?

It's true that this request does not do multiple things concurrently; making it async does not speed up the request at all.

However, the application as a whole does have other things to do; specifically, it has other requests it can respond to. Using async frees up thread pool threads whenever they're not being actively used. This allows your application to scale (assuming your backend is scalable). In other words, async allows you to make maximum use of the thread pool.

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

Comments

0

It uses aysnc because connecting to the database and getting a response could take some time and if it was done synchronously the application would be locked in a waiting state all that time.

By making the call asynchronous the application is free to do something else while the request is sent to the database and the data returned.

Comments

-3

If you mark a function as async and then call it using the await-keyword at another point in your program, then the thread which called the async method doesn't block the execution of your program and can get suspended until it receives your answer.

That way i.e. your whole UI doesn't freeze up while you are pulling data from your database.

2 Comments

This answer would be perfectly correct if this were a client application. However, for ASP.NET, this answer is wrong; async does not cause an HTTP response to be sent.
@StephenCleary oh, you are right; I totally missed the ASP.NET-tag. That way, my answer isn't really fitting...

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.