. Net asynchronous programming async & amp; await keyword,. net asynchronous async

Source: Internet
Author: User

. Net asynchronous programming async & await keyword,. net asynchronous async

C #5.0 introduces two keywords async and await, which greatly help us simplify the implementation code of asynchronous programming, in addition, tasks in TPL are closely related to async and await.

I thought about the async & await keywords in asynchronous programming, and I am confused about the two keywords, especially the await keywords. So I thought deeply. The following is an example:
[Asynchronous programming you must know] C #5.0 new features -- Async and Await make asynchronous programming easier
This is written by a big bull in the blog Park. I have been paying attention to this great god. I have to say that there are many great gods in the blog Park and the atmosphere is also good. I introduced the following example:

Private async void button#click (object sender, EventArgs e) {var length = AccessWebAsync (); // here you can perform some operations OtherWork (); this. textBox1.Text + = String. format ("\ n reply Byte Length: {0 }. \ r \ n ", await length); this. textBox2.Text = Thread. currentThread. managedThreadId. toString ();} private async Task <long> AccessWebAsync () {MemoryStream content = new MemoryStream (); // initiate a Web Request HttpWebRequest webRequest = WebRequest to MSDN. create (" http://msdn.microsoft.com/zh-cn/ ") As HttpWebRequest; if (webRequest! = Null) {// return the response result using (WebResponse response = await webRequest. getResponseAsync () {using (Stream responseStream = response. getResponseStream () {await responseStream. copyToAsync (content) ;}} this. textBox3.Text = Thread. currentThread. managedThreadId. toString (); return content. length;} private void OtherWork () {this. textBox1.Text + = "\ r \ n waiting for the server to reply ................. \ n ";}

Async is a synchronous execution program, while await is used to divide fragments and suspend the caller, and does not create new threads. According to the analysis by the experts:

The first part of the code and the subsequent part of the code displayed in the await keyword are both executed synchronously (that is, the code is executed on the call thread, that is, the GUI thread, so there is no cross-thread access control problem ), the code snippet at the key of await is executed on the thread pool thread.

In the above Code, methods such as GetResponseAsync encapsulated by FCL are called so as not to block the current UI thread. await does not create a new thread, but here, the await expression does create a new thread -- GetResponseAsync. this creates the illusion of surface synchronization. I have written an article before.
C # Notes on async and await asynchronous programming

The await keyword is highly correlated with the Task. It can be seen from its specific return value that the deeper level of await should be equivalent to the continuewith function of the task. therefore, use the async & await keywords to implement Asynchronization. Either call the FCL encapsulated Asynchronous Method, or call the task to create a new thread to share the tasks of the UI thread to prevent UI thread blocking.

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.