Await, Anync

Source: Internet
Author: User

 PublicForm1 () {InitializeComponent (); }        //The following method runs asynchronously. The UI thread is not//blocked during the delay. You can move or resize the Form1 window//While Task.delay is running.         Public Asynctask<string>Waitasynchronouslyasync () {awaitTask.delay (10000); MessageBox.Show ("fi"); return "finished"; }        //The following method runs synchronously, despite the use of async. //You cannot move or resize the Form1 window while Thread.Sleep//is running because the UI thread is blocked.         Public Asynctask<string>waitsynchronously () {//ADD a using directive for system.threading.Thread.Sleep (10000); return "finished"; }        Private Async voidButton1_click_1 (Objectsender, EventArgs e) {            stringresult =string.            Empty;            Waitasynchronouslyasync (); //Call the method, that runs asynchronously. //result = await waitasynchronouslyasync (); //Call the method, that runs synchronously. //result = await waitsynchronously (); //Display the result.TextBox1.Text + =result; MessageBox.Show ("Hi"); }
The code above concludes at least two points:
1. Await Waitasynchronouslyasync () to wait for the call to complete before the subsequent code, but before the call is complete, its thread can be consumed by other code, the performance is that the UI can still receive other messages
2. The Waitasynchronouslyasync () function executes the same thread as the UI thread, which is why the MessageBox in this function can be displayed, Thread.Sleep can prevent the UI thread from receiving messages

Await, Anync

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.