async await tutorial: https://www.cnblogs.com/zhaoshujie/p/11192036.html
Asynchronous methods:
private async Task<string> TestAsync() { Console.WriteLine ( " Asynchronous method begins " ); var Task Task.Run = (() => { Thread.Sleep(2000); Console.WriteLine ( " new thread " ); return " new thread end " ; }); var result = await task; Console.WriteLine ( " Asynchronous method ends " ); return result; }
When not in use await call
public IActionResult Index() { Console.WriteLine ( " main thread start " ); Console.WriteLine ( " output: " + TestAsync ()); Console.WriteLine ( " main thread end " ); }
result:
1, the main thread ++ code prior to await the asynchronous method executed in the order
2, await the start (open a new thread)
Get value TestAsync (); a: 3, the main thread remaining code execution, Console.WriteLine (+ TestAsync () "output")
4, await the end, the code continues await asynchronous method
Use await when calling
public async Task<IActionResult> Index() { Console.WriteLine ( " main thread start " ); Console.WriteLine ( " output: " + the await TestAsync ()); Console.WriteLine ( " main thread end " ); }
result:
All the execution order, return can get the correct value