Async and Await in Windows Store Application

Before reading this article, please go through the following articles-
  1. Developing Windows Store Apps: Introduction And Requirement Specification
  2. Plans For Designing Metro Style Apps (Windows Store Apps)
  3. Windows Store Application Life-Cycle
  4. Guidelines to Pass Windows Store App Certification
  5. Navigation Patterns to Develop Windows Store Application
  6. Templates to Develop Windows Store Apps
  7. Develop Your First Windows Store App (Hello World)
  8. Controls to Design Windows Store Apps
  9. Lay-out Design Guidelines For Windows Store Apps
  10. Guidelines to Create Splash Screen For Windows Store Application
  11. Configure Your Windows Store Application Using Manifest Designer
  12. Working With Progress Controls in Windows Store Application
  13. Global AppBar For Windows Store Applications
  14. Using Webcam to Preview Video In Windows Store App
  15. Rotate Captured Video in Windows Store App

Introduction

 
In my last article, we saw video rotation in a Windows Store app. In this article, we will see some asynchronous programming in a Windows Store app. We are all familiar with async programming using delegates but that is a very critical process that makes our code difficult.
 
In Windows 8 Microsoft has provided a new way to do async programming that is very easy. As you see I'd given the title to this article as "Async and Await" because in Windows Store we have these two keywords that make our async programming work very easy. By using those two keywords we can make async method calls.
 

What is Asynchronous?

 
I hope all of you are aware of what asynchronous is. When we have some methods that require a long time to execute then we can call those methods asynchronously. For example, take the following method.
  1. Private void SomeMethod ()  
  2. {  
  3. Thread.Sleep (5000);  
  4. }   
The method above delays the thread; in other words, it takes extra time to execute. If we call this method synchronously then our user must wait until execution completes. In this case, the application UI is also not responsive; in other words, the user cannot work with other elements of the application UI. But if we call this method asynchronously then our user does not need to wait until the method execution completes. When we call any method asynchronously then it executes in a different thread while the main thread is still running.
 

Async made easy in Windows Store application

 
As we discussed in earlier articles in this series Microsoft has provided a new programming interface with Windows 8. In the new way of programming in Windows 8 Microsoft has made async programming very easy by using async and await keywords.
 
If you are aware of the async programming using delegates then you know how critical the code is that we must write for doing async operations. For example, the following simple sample calls one method asynchronously using delegates.
  1. private delegate void SomeDelegate(int input1, int input2);  
  2. private int SomeMethod(int input1, int input2)  
  3. {//method implementation }  
  4. private void SomeCallback(IAsyncResult ar)  
  5. {  
  6.     //Implementation  
  7. }  
  8. protected void btncal_Click(object sender, EventArgs e)  
  9. {   
  10.     SomeDelegate deleg = new SomeDelegate (SomeMethod);  
  11.     int input1 = Convert.ToInt16(tbinput1.Text);  
  12.     int input2 = Convert.ToInt16(tbinput2.Text);  
  13.     IAsyncResult ar = deleg.BeginInvoke(input1, input2,new AsyncCallback(SomeCallback), null);         
  14. }   
If you see in the code above, we can understand for one simple async call we must write many lines of code. But in Windows Store applications this is very easy using the asynch and await keywords. Have a look at the following snippet.
  1. private async void Button_Click_1(object sender, RoutedEventArgs e)   
  2. {  
  3.     Windows.Web.Syndication.SyndicationClient client = new SyndicationClient();     
  4.     Uri feedUri  
  5.         = new Uri ("http://www.c-sharpcorner.com/rss/latestarticles.aspx");  
  6.     try  
  7.     {  
  8.         SyndicationFeed feed = await client.RetrieveFeedAsync (feedUri);  
  9.         foreach (SyndicationItem item in feed.Items)  
  10.         {  
  11.             rssOutput.Text += item.Title.Text + ", " +  
  12.                              item.PublishedDate.ToString() + Environment.NewLine;  
  13.         }  
  14.     }  
  15.     catch (Exception ex)  
  16.     {  
  17.     }  
  18. }    
In the code above you can see how easy it is to make async calls. In the example above we are calling the RSS feed from c-sharpcorner. We are downloading the latest article feed from c-sharpcorner that requires more time to download the feed.
 
In the code above you can see in the button click event handler, I've given the asynch keyword. In Windows 8 when you need to perform an operation asynchronously then we must add the asynch keyword. Next, you will see on this line "SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri)" in which we used the await keyword that tells the compiler that this is an asynchronous call. When the await keyword is used the compiler does more for you. We will not discuss what the compiler does for you in more detail. When we call the line above using the await keyword the handler execution stops but our UI is still responsive and the user can perform other activities on our application.
 
Next, you can see in the earlier snippet that if we must get a result from the asynch call then we must write a CallBack function but in Windows Store, we do not need to do that because when the asynch execution completes and we have the result then the application enters the handler again and then resumes execution.
 

Conclusion

  
From the preceding discussion, I hope you understand how easy it is to make asynchronous programming in the Windows Store application.