Win8.1 asynchronous programming for application development

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.