Before reading this article, please go through the following articles-
- Developing Windows Store Apps: Introduction And Requirement Specification
- Plans For Designing Metro Style Apps (Windows Store Apps)
- Windows Store Application Life-Cycle
- Guidelines to Pass Windows Store App Certification
- Navigation Patterns to Develop Windows Store Application
- Templates to Develop Windows Store Apps
- Develop Your First Windows Store App (Hello World)
- Controls to Design Windows Store Apps
- Lay-out Design Guidelines For Windows Store Apps
- Guidelines to Create Splash Screen For Windows Store Application
- Configure Your Windows Store Application Using Manifest Designer
- Working With Progress Controls in Windows Store Application
- Global AppBar For Windows Store Applications
- Using Webcam to Preview Video In Windows Store App
- Rotate Captured Video in Windows Store App
Introduction
What is Asynchronous?
- Private void SomeMethod ()
- {
- Thread.Sleep (5000);
- }
Async made easy in Windows Store application
- private delegate void SomeDelegate(int input1, int input2);
- private int SomeMethod(int input1, int input2)
- {//method implementation }
- private void SomeCallback(IAsyncResult ar)
- {
- //Implementation
- }
- protected void btncal_Click(object sender, EventArgs e)
- {
- SomeDelegate deleg = new SomeDelegate (SomeMethod);
- int input1 = Convert.ToInt16(tbinput1.Text);
- int input2 = Convert.ToInt16(tbinput2.Text);
- IAsyncResult ar = deleg.BeginInvoke(input1, input2,new AsyncCallback(SomeCallback), null);
- }
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.
- private async void Button_Click_1(object sender, RoutedEventArgs e)
- {
- Windows.Web.Syndication.SyndicationClient client = new SyndicationClient();
- Uri feedUri
- = new Uri ("http://www.c-sharpcorner.com/rss/latestarticles.aspx");
- try
- {
- SyndicationFeed feed = await client.RetrieveFeedAsync (feedUri);
- foreach (SyndicationItem item in feed.Items)
- {
- rssOutput.Text += item.Title.Text + ", " +
- item.PublishedDate.ToString() + Environment.NewLine;
- }
- }
- catch (Exception ex)
- {
- }
- }
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.

Join the conversation! Your thoughts help the community grow.