In my previous ,post I had introduced the concept of the wait, wait all, wait any. Lets see a few of these in action. More over lets learn how to efficiently convert our own synchronous code to take the benefit asynchronous and parallel methodologies.
implementation details,or parsing of XML.If you want to see that, you can just download the code.
So Lets click “BBC News” and then “Google” Now, in the figure above you can see 2 numbers have appeared below the textbox. The numbers indicate the amount of milliseconds it took for the respective servers to reply with a response. As you can see , Google servers have a quite high latency of more than a second. The numbers in the textbox indicate the number of headlines.
Now the “Both” Button, synchronously fires both the requests one after the other .The typical time it takes is to the tune of almost 3000 miliseconds.
Now lets see the code. The code or the BBC button is as follows .
int time_start = System.Environment.TickCount;
String url = “http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk#”;
WebResponse Response = getResponse(url);
textBox1.Text = processBBCNews(Response).ToString();
int time_stop = System.Environment.TickCount;
label1.Text = (time_stop – time_start).ToString();
Now, the code for Google News button is exactly same only the URL is different and the updation labels are different.
getResponse is an user defined function that sends the Http Get request and receives the response.processBBCNews extracts the title from the XML and puts each one into the List.
.In the code behind for the “both” button, we do exactly the something. We first send the HTTP get request for the Google News, then receive the response and process it. Then we fire off another GET Http request for BBC news and get the response. The total time taken is noted.
Now to convert synchronous code to asynchronous code
//As we know, that processing of results can only occur when the Response is
//recieved ,hence instead of using
//callbacks we are using continue with.In my series,I will you to think in terms
//of continuations and not callbacks
//Here antecedent refers to the task thats just finished .
All the explanations are given through the comments .I will share my code in the next blog post where we look at the code behind of the “Both” button.