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