Saturday, August 22, 2015

Await and Async keywords in .net 4.5

Moving to .NET 4.5, Async and Await key words are introduced for Asynchronous Programming in C#. Let's have a quick look on what these are.

Async and awit are markers which mark code positions from where control should resume after a task (thread) completes.

See the following example,

1. It goes from Main to Method() and invoked LongTask()
   in a multi threaded way.
2. And code then goes ahead "COnsole.WriteLine("New thread")"



But we want to wait LongTask() to run and then exeute  "Console.WriteLine("New thread")".

So "COnsole.WriteLine("New thread")" this should wait untill LongTask() is done.

So this is what you do with above keywords,



Now , 
after LongTask() is done, "Console.WriteLine("New thread")" line is executed means it waits till 20s and show "New thread" in the console.

Hope you have got the point. !!

reference : www.questpond.com