次の方法で共有


.Net : Asynchronous Programming Techniques

Hi There,

I am Syam Pinnaka, Dev in IAM services team at Microsoft.

During the past two weeks, I was trying to explore the .Net Asynchronous programming and so thought I would share my learning. In general, there are two different ways of Asynchronous programming in .Net. 1) Using IAsyncResult 2) using event based Asynchronous pattern. Lets see how to use IAsyncResult in this blog post and I will try and cover Event based Asynchronous pattern in my next post.

Some functionality in .Net can be called from client application either synchronously or asynchronously. Webservice calls are a common example for this. Loading the high resolution  images in a web page could be another good scenario for this. Whenever an asynchronous call is supported, these methods typically start with BeginMethodname and EndMethodName. BeginMethodName typically have signature similar to its synchronous variant with two additional parameters AsyncCallback and object state. AsyncCallback can be used to pass in the callback method for when the asynchronous call is completed and object state is used to pass in some custom data to the asynchronous call. In case when multiple asynchronous calls are made, this state can be used to differentiate these various client calls. A typical begin signature and client call would look like below.

 public IAsyncResult BeginWriteLine(string line, AsyncCallback callback, object state) { ... }
  
 Client call: 
 IAsyncResult result = writelineToLogfile.BeginInvoke(line, callback, state);

The return value from the above call is IAsyncResult which can be used to track and wait for Asynchronous call to complete. IAsyncResult has IsCompleted property which will tell whether the Asynchronous call is completed or not. IsCompleted can also be used in scenario like Polling technique where in the client can keep checking the return value of IsCompleted until the Asynchronous call is completed. There is also AsyncWaitHandle property which gives us a WaitHandle object which can be used to wait for the Asynchronous operation to complete. WaitHandle can also be used in scenario when the client will need to wait until all Asynchronous calls or a specific set of Asynchronous calls are completed. Variations of with WaitAll or WaitAny can be used for this.

To retrieve the return value when the Asynchronous operation completes, Call the EndMethodName by passing in the IASyncResult as input.

To block the client application execution until the Asynchronous call is completed, use either 1)  AsyncWaitHandle or  2) Call EndMethodName. EndMethodName will block the application execution until the Asynchronous call is completed.

The client applications which do not need to wait until Asynchronous call is completed can either use 1) polling technique or 2) provide an EndCallback which will then be called when the Asynchronous operation is completed.

Best of all, .Net also provides a means by which any synchronous call can be made Asynchronous using delegates. This in itself is a good topic and I will try covering this in a separate blog post.

That’s it for now and I will cover Event based Asynchronous programming in my next post.

Happy coding and Happy new year 2012!

 

Technorati Tags: .Net,Async Programming