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
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 makes 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:
Private void SomeMethod ()
{
Thread.Sleep (5000);
}
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.
private delegate void SomeDelegate(int input1, int input2);
private int SomeMethod(int input1, int input2)
{//method implemetation }
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 complier 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 Windows Store application.