In Win8 App Store development, we encounter many asynchronous methods that exist to ensure that your app still responds well when executing tasks that require a lot of time, meaning that the asynchronous API is invoked in response to the user's actions. Imagine that we clicked on a button that would download some information from the Internet, and if it wasn't, we'd have to wait until it was downloaded to continue. To be able to remain responsive while downloading, Windows provides an asynchronous method Syndicationclient.retrievefeedasyncfor downloading the source.
Put the keyword, async on the declaration of the event handler.private async void Button_click_1 (object sender, Routede Ventargs e) {Windows.Web.Syndication.SyndicationClient client = new Syndicationclient (); Force the syndicationclient to download the information. Client. Bypasscacheonretrieve = true; Uri Feeduri = new Uri ("http://windowsteamblog.com/windows/b/windowsexperience/atom.aspx"); try {//call syndicationclient retrievefeedasync to download the list of blog posts. SyndicationFeed feed = await client. Retrievefeedasync (Feeduri); The rest of this method executes after await Retrievefeedasync completes. Rssoutput.text = feed. Title.text + Environment.NewLine; foreach (SyndicationItem item in feed. Items) {Rssoutput.text + = Item. Title.text + "," + Item. Publisheddate.tostring () + Environment.NewLine; }} catch (Exception ex) {//Log Error. Rssoutput.text = "I ' m sorry, but I couldn ' t load the page," + "possibly due to network problems." + "Here ' s the error message I received:" + ex. ToString (); }}
The name of the Async method ends with async and requires an operator await when invoking the Async method, telling the compiler that this is an asynchronous method, and be aware that you want to add the keyword async to the declaration of the method that uses the await operator, such as Button_click_1.
In fact, the execution flow of the above program is: when the execution of an asynchronous method to await, the code after await waits until the asynchronous method completes and returns to execute, but during asynchronous method execution, we can still interact with other functions of the application.