A-synchronism in Windows 8 App Development

This will be the last of our series Asynchronous programming.Now since we have a strong background of tasks,this will be very easy to grasp since from C# 5.0,asynchronous has been heavily simplified to represent closely its synchronous counter part.

We have two new keywords called async and await which we will be using.I will be referring to my previous posts .

Our demo app looks like this .The third column having textblocks for comments


Scenario 1 : Heavy work without using await .

So lets see the code for the No Await button

   1: start2.Text = System.Environment.TickCount.ToString() ;

   2:            AsyncDownloadFeed();

   3:            stop2.Text = System.Environment.TickCount.ToString();

AsyncDownloadFeed downloads news off the BBC website and also runs a synchronous piece of loop

Lets look at the code behind AsyncDownloadFeed.

   1: public async void AsyncDownloadFeed()

   2:         {

   3:             String url = "http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk#";

   4:             HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(url);

   5:             Task<WebResponse> response =  WebRequestObject.GetResponseAsync();

   6:             resp = await response;

   7:             comments_Copy.Text = "Loop Starting";

   8:            insaneSynchronousLongLoop();

   9:            comments_Copy.Text = "Loop Finished";

  10:             processNews();

  11:         }

yes I know I have used await here but bear with me.Treat this function as a blackbox.Next is the synchronous loop

   1: private  async void  insaneSynchronousLongLoop()

   2:         {

   3:             // comments.Text = "I am in the loop";

   4:             comments.Text = "I am in the loop";

   5:             int i = 0;

   6:             for (i = 0; i < 1000000000; i++) ;

   7:             comments.Text = "loop finished";

   8:         }

Click on the No await button and we will see, the difference between start time and end time is only 20 miliseconds apart.As expected,the textblocks populates even before the function AsyncDownloadFeed is completed.


If we wait a few seconds, we will finally see the result.


We already know this phenomenon as its not waiting for the function call to finish.

1.Why does it not wait for the function? It should have been synchronously waiting.?

Answer: Using the “async” keyword isnt the what causing the asynchronous behaviour.The async keyword single handedly cannot be used to transform any synchronous code into non asynchronous one.Async keyword just informs the compiler that there may be code which would need to be run asynchronously. So what is the cause ? Lets examine the AsyncDownloadFeed function by providing two outputs.

   1: public async void AsyncDownloadFeed()

   2:         {


   4:             String url = "http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk#";

   5:             HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(url);

   6:             Task<WebResponse> response =  WebRequestObject.GetResponseAsync();

   7:             Debug.WriteLine("Started Awaiting");

   8:             var start = System.Environment.TickCount;

   9:             resp = await response;

  10:             var stop = System.Environment.TickCount;

  11:             Debug.WriteLine("End Await Duration="+(stop-start).ToString());


  13:             Debug.WriteLine("Loop Starting");

  14:             insaneSynchronousLongLoop();

  15:             Debug.WriteLine("Loop Ending");

  16:             processNews();

  17:         }

Task<WebResponse> response =  WebRequestObject.GetResponseAsync(); is familiar since we already are acquainted with TPL.

resp = await response; is the place where the magic occurs.This tells the compiler that it needs to wait for the response but in such a way,so that it does not block the UI.But this is not similar to “wait” as not only does it not block the UI,the compiler moves on to the next statement .Which means,the insaneSynchronousLongLoop() runs even before the webresponse is returned.Moreover in the insaneSynchronousLongLoop does not block the UI since its running on a different thread.

Another thing is ,we can manipulate the UI within the insaneSynchronousLongLoop () since the compiler takes care of that updating the UI only when the threads return to the main UI.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s