Search This Blog

Tuesday, March 25, 2014

Async Programming In C#

An Introduction to Async Programming In C#

The Async feature in Visual Studio 2012 RC makes it easy to invoke asynchronous methods.If a method marked with or async modifier, we can use the await operator in the method. When control reaches an await expression in the async method, control returns to the caller, and progress in the method is suspended until the awaited task completes. When the task is complete, execution can resume in the method.
Using asynchronous methods instead of synchronous methods can provide benefits.  Asynchrony makes UI applications more responsive because the UI thread that launches that operation can perform other work.  Asynchrony also improves the scalability of server-based application by reducing the need for threads.

What's the difference between asynchronous programming and using multiple threads to do parallel programming?
In synchronous programming, something doesn't happen until the last thing is done. There may not be any human delays for which Async may be useful, but there are much shorter delays. Think milliseconds. These often are I/O operations like waiting to retrieve something from the hard drive. There are a lot of short delays, so the milliseconds add up to mega-milliseconds, so people watch swirling donuts.
Using multiple threads and the Task Parallel Library can improve things. On a single core processor, while one thread is waiting to get something back from the hard drive, Windows may assign another thread to run on the processor. That makes the whole thing run faster. On multiple core processors, things are even better. Windows may assign one thread to run on one core, and another to run on another.

No comments:

Post a Comment