. NET Async Programming Async & await keyword thinking

Source: Internet
Author: User

C # 5.0 introduces two keywords async and await, and these two keywords help us to a great extent simplify the implementation code for asynchronous programming, and the task in TPL is very much related to async and await.

Thinking about the async & await keyword in asynchronous programming, the two keywords, especially the await keyword, have been confusing, so think deeply. The first examples are:
[Asynchronous programming you must know] C # 5.0 new features--async and await make asynchronous programming easier
This is a blog park a Daniel wrote, himself has always been concerned about this great God, have to say, the blog Park Great God, and the atmosphere is also very good. I've introduced an example of one of these:

Private async void Button1_Click (object sender, EventArgs e) {var length = Accesswebasync ();            Here can do some do not rely on the response of the Operation 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 (); Initiating a Web request to MSDN HttpWebRequest WebRequest = webrequest.create ("http://msdn.microsoft.com/zh-cn/") a            s HttpWebRequest; if (webRequest! = null) {//returns the reply result using (WebResponse response = await webreques T.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 server reply ... \ n"); }

Async is the synchronous execution of the program, and the await is divided into fragments and suspend the role of the caller, and does not create a new thread, according to the Great God Analysis:

The first part of the code that appears in the await keyword and the code that follows it are all synchronous (that is, the GUI thread that executes on the calling thread, so there is no problem with cross-threading access to the control), and the code snippet at the await key is executed on the thread pool threads.

In the above code, such methods as the FCL encapsulated Getresponseasync are called so that the current UI thread is not blocked, and await does not create a new thread, but here the await expression does create a new thread-- What Getresponseasync did. That caused the illusion of surface synchronization. I've written an article before.
C#async and await asynchronous programming learning notes

The await keyword has a great association with a task, and it can be seen from its specific return value that a deeper await is equivalent to the ContinueWith function of the task. So, with Async & The AWAIT keyword implementation asynchronously either calls the FCL encapsulated async method, or we call the task ourselves to create a new thread to share the task of the UI thread to prevent the UI thread from blocking.

. NET Async Programming Async & await keyword thinking

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.