Saturday, February 19, 2011

Parallel Programming .NET 4.0 IV–Using Task.Factory.FromAsync convert Task to asynchronous Method.

In this blog post we are going to see how we can covert asynchronous method call to Task by using Task.Factory.FromAsync method.

For this example I am going to call the following  Web service  to get the weather information for given Zip code.

image

when adding this above Web Service Reference we need to check  Generate asynchronous Operations. so that VS.NET will generate Async method calls. 

image

First Create Instance of the web service Proxy. and Create the Task with Begin and End methods which you are going to invoke on Web Service.  Basically this web service takes input as Zip code and returns as 1 week Weather forecast info  .

image

 

Here is the weather result for the Zipcode =75023

image

suppose Web service response is null then I can canceling the task .  here is the result for unknown zip code.

image

Here I am scheduling a continuation task once we get the response from Web Service. I feel this way of calling Web Service is more convenient than an IAsyncResult.

Nandri(Thanks)

SreenivasaRagavan.

No comments: