2:C#TPL Quest

Source: Internet
Author: User

Theory:

1, as long as the method is the return value of the task type, you can use await to wait for the call to get the return value.

2. If a method that returns a task type is marked with async, it is possible to return an instance of this type directly within the method.

3. A method that returns a task type is not marked with async, then a method internal direct Task instance is required.

Practice (Add a button yourself):

Private Async voidButton1_Click (Objectsender, EventArgs e) {            #regionMethod One://string i1 = await f1async ();//ensure that the asynchronous task finishes executing//MessageBox.Show ("i1=" + i1); //string i2 = await f2async (); //MessageBox.Show ("i2=" + i2);            #endregion            #regionMethod Two:Task<string> Task1 =F1async (); Task<string> Task2 =F2async (); stringI1 =awaitTask1;//this guarantees that the TASK1 task must be executed, and it is not here to perform the asynchronous task. MessageBox.Show ("i2="+i1); stringI2 =awaitTask2; MessageBox.Show ("i2="+i2); #endregion        }        Statictask<string>F1async () {MessageBox.Show ("F1 Start"); returnTask.run (() ={System.Threading.Thread.Sleep ( +); MessageBox.Show ("F1 Run"); return "F1";        }); }        Statictask<string>F2async () {MessageBox.Show ("F2 Start"); returnTask.run (() ={System.Threading.Thread.Sleep ( -); MessageBox.Show ("F2 Run"); return "F2";        }); }
View Code

2:C#TPL Quest

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.