DCSIMG
Asynchronous programing in C# - Hagit Badash

Asynchronous programing in C#

Recently Microsoft presented a glimpse to the C# 5 by presenting the Visual Studio Async CTP.

This new feature allows you to develop asynchronous applications in a very simple way.

In the past when you called to an asynchronous method you had to split your code to several segments the code before the async call, the code after the async call completed successfully and the code after the async call failed. Using the visual studio Async feature you do not need to do so anymore. The code of the application become much clearer and easier to write.

All you need to do is define the asynchronous  methods as async and use the await keyword to start an async method call as in the following example:

 async void Init()    
{          
     EvaluationData newData = await InitSpecificInformation();
     // Perform operations after InitSpecificInformation completion
}

Task<EvaluationData> InitSpecificInformation()    
{
     return TaskEx.Run(() =>
         {
             EvaluationData data = new EvaluationData();
             // Perform init operations
             Thread.Sleep(10);
             return data;
         });
}   
 
 
Published Sunday, November 21, 2010 10:47 PM by hagb

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems