Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Use Async/Await Over Normal Threading or Tasks?

Tags:

c#

.net

I've been reading a lot about async and await, and at first I didn't get it because I didn't properly understand threading or tasks. But after getting to grips with both I wonder: why use async/await if you're comfortable with threads?

The asynchronousy of async/await can be done with Thread signaling, or Thread.Join() etc. Is it merely for time saving coding and "less" hassle?

like image 724
James Jeffery Avatar asked Aug 18 '13 12:08

James Jeffery


People also ask

Why async is better than multithreading?

Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.

What is the advantage of using async await?

The biggest advantage of using async and await is, it is very simple and the asynchronous method looks very similar to a normal synchronous methods. It does not change programming structure like the old models (APM and EAP) and the resultant asynchronous method look similar to synchronous methods.

What is the point of async await?

async and await The await operator tells the compiler that the async method can't continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method.

Does async await improve performance?

C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.


Video Answer


1 Answers

Yes, it is a syntactic sugar that makes dealing with threads much easier, it also makes the code easier to maintain, because the thread management is done by run-time. await release the thread immediately and allows that thread or another one to pick up where it left off, even if done on the main thread.

Like other abstractions, if you want complete control over the mechanisms under the covers, then you are still free to implement similar logic using thread signaling, etc.

If you are interested in seeing what async/await produces then you can use Reflector or ILSpy to decompile the generated code.

Read What does async & await generate? for a description of what C# 5.0 is doing on your behalf.

like image 123
Karl Anderson Avatar answered Oct 13 '22 11:10

Karl Anderson