C # Learning record 6--Async async and await

Source: Internet
Author: User

Async can declare asynchronous processes.

The method is generally declared as async, in which there is an await content

1 Private Async voidStartbutton_click (Objectsender, RoutedEventArgs e)2 {3 4Resultstextbox.text + ="\ n";5     Try6     {7         intLength =awaitExamplemethodasync ();8Resultstextbox.text + = String.Format ("Length: {0}\n", length);9     }Ten     Catch(Exception) One     { A         //Process The exception if one occurs. -     } - } the  -  Public Asynctask<int>Examplemethodasync () - { -     varHttpClient =NewHttpClient (); +     intExampleint = (awaitHttpclient.getstringasync ("http://msdn.microsoft.com")). Length; -Resultstextbox.text + ="Preparing to finish examplemethodasync.\n"; +  A     returnExampleint; at } - //Output: - //Preparing to finish examplemethodasync. - //length:53292

When declaring a method, marking it as async indicates that there is an await asynchronous content in the method.

When you call async, you use the await keyword.

In the Async method, the execution continues to the await section, when the method is suspended, other content is waiting for it to complete, and the part associated with it is completed.

If the program involves downloading data from the network, reading a large amount of content from the file, and doing more of the behavior, setting it to execute asynchronously can be a good way to make the whole program smoother.

Example source and reference link: https://msdn.microsoft.com/zh-cn/library/hh156513.aspx

Https://msdn.microsoft.com/zh-cn/library/hh191443.aspx

Both 2.double.parse and Double.tryparse can convert a string to Double. For example, "12345" can be converted to a number of 12345

From the name, TryParse is an indeterminate conversion, which is used when you are unsure whether a string can be converted to a double.

Of course, an error can also occur if a string passed into parse cannot be converted to a double.

The difference between the two is

The TryParse return value type is Boolean, which means that if it cannot be converted, it returns false directly.

The parse will be converted directly, if unable to convert, will throw exception, internal if cannot solve, will throw method.

So, TryParse is generally used in statements such as if (Doublt. TryParse (str)) {...}; The parse is assigned directly, for example: Double db=double. Parse (str);

More often, TryParse is used in another usage: double db; if (Doublt. TryParse (str, out db)) {...}; If the conversion succeeds, the DB is assigned the value

C # Learning record 6--Async async and await

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.