Async和Await
1 public class AwaitAsyncShow 2 { 3 public static void Show() 4 { 5 Test(); 6 } 7 private async static Task Test() 8 { 9 Console.WriteLine($"当前主线程id={Thread.CurrentThread.ManagedThreadId}"); 10 { 11 //NoReturnNoAwait(); 12 } 13 { 14 //NoReturn(); 15 //for (int i = 0; i < 10; i++) 16 //{ 17 // Thread.Sleep(300); 18 // Console.WriteLine($"Main Thread Task ManagedThreadId={Thread.CurrentThread.ManagedThreadId} i={i}"); 19 //} 20 } 21 { 22 //Task t = NoReturnTask(); 23 //Console.WriteLine($"Main Thread Task ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 24 //t.Wait();//主线程等待Task的完成 25 //await t;//await后的代码会由 子线程执行 26 } 27 { 28 Task<long> t = SumAsync(); 29 Console.WriteLine($"Main Thread Task ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 30 long lResult = t.Result;//访问result 主线程等待Task的完成 31 t.Wait();//等价于上一行 32 } 33 34 Console.Read(); 35 } 36 37 /// <summary> 38 /// 只有async没有await,会有个warn 39 /// 跟普通方法没有区别 40 /// </summary> 41 private static async void NoReturnNoAwait() 42 { 43 //主线程执行 44 Console.WriteLine($"NoReturnNoAwait Sleep before Task,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 45 Task task = Task.Run(() => 46 { 47 Console.WriteLine($"NoReturnNoAwait Sleep before,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 48 Thread.Sleep(3000); 49 Console.WriteLine($"NoReturnNoAwait Sleep after,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 50 }); 51 52 //主线程执行 53 Console.WriteLine($"NoReturnNoAwait Sleep after Task,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 54 } 55 56 /// <summary> 57 /// async/await 58 /// 不能单独await 59 /// await只能放在task前面 60 /// 不推荐void返回值,使用Task来代替 61 /// Task和Task<T>能够使用await,Task.WhenAny,Task.WhenAll等方式组合使用。Async Void 不行 62 /// </summary> 63 private static async void NoReturn() 64 { 65 //主线程执行 66 Console.WriteLine($"NoReturn Sleep before await,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 67 TaskFactory taskFactory = new TaskFactory(); 68 Task task = taskFactory.StartNew(() => 69 { 70 Console.WriteLine($"NoReturn Sleep before,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 71 Thread.Sleep(3000); 72 Console.WriteLine($"NoReturn Sleep after,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 73 }); 74 await task;//主线程在这里就返回来,执行主线程任务去了 75 76 //耗时任务就用await 77 78 //子线程执行 其实是封装成委托,在task之后成为回调(编译器功能,状态机实现) 79 //task.ContinueWith() 80 //这个回调的线程是不确定的:可能是主线程,可能是子线程,也可能是其他线程 81 Console.WriteLine($"NoReturn Sleep after Task,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 82 } 83 84 /// <summary> 85 /// 无返回值 async Task==async void 86 /// Task和Task<T>能够使用await,Task.WhenAny,Task.WhenAll等方式组合使用。Async void不行 87 /// </summary> 88 /// <returns></returns> 89 private static async Task NoReturnTask() 90 { 91 //这里是主线程的Id 92 Console.WriteLine($"NoReturnTask Sleep before await,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 93 await Task.Run(() => 94 { 95 Console.WriteLine($"NoReturnTask Sleep before,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 96 Thread.Sleep(3000); 97 Console.WriteLine($"NoReturnTask Sleep after,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 98 }); 99 //await task 100 Console.WriteLine($"NoReturnTask Sleep after await,ThreadId={Thread.CurrentThread.ManagedThreadId}"); 101 102 //return new TaskFactory().StartNew(() => { });//不能return 没有async才行 103 } 104 105 /// <summary> 106 /// 待返回至的Task 要使用返回值就一定要等到子线程计算完毕 107 /// </summary> 108 /// <returns></returns> 109 private static async Task<long> SumAsync() 110 { 111 Console.WriteLine($"SumAsync 111 start ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 112 long result = 0; 113 114 await Task.Run(() => 115 { 116 for (int i = 0; i < 10; i++) 117 { 118 Console.WriteLine( 119 $"SumAsync {i} await Task.Run ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 120 Thread.Sleep(1000); 121 } 122 123 for (long k = 0; k < 999999999; k++) 124 { 125 result += k; 126 } 127 }); 128 Console.WriteLine($"SumFactory 111 end ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 129 return result; 130 } 131 132 /// <summary> 133 /// 真的返回Task 不是async 要使用返回值就一定要等子线程计算完毕 134 /// </summary> 135 /// <returns></returns> 136 private static Task<int> SumFactory() 137 { 138 Console.WriteLine($"SumFactory 111 start ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 139 TaskFactory taskFactory=new TaskFactory(); 140 Task<int> iResult= taskFactory.StartNew<int>(() => 141 { 142 Thread.Sleep(3000); 143 Console.WriteLine( 144 $"SumFactory 123 Task.Run ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 145 146 return 123; 147 }); 148 Console.WriteLine($"SumFactory 111 end ManagedThreadId={Thread.CurrentThread.ManagedThreadId}"); 149 return iResult; 150 } 151 }