使用Async与Await进行异步编程 简单代码
private async void AsyncMeth() { for (int i = 0; i < 5; i++) { await Task.Delay(2000); Console.WriteLine(i.ToString()); } var res = await Add(1, 2); var a = (int)res; Console.WriteLine(a); } private async Task<int> Add(int a, int b) { return a + b; }
Console.WriteLine("主线程测试开始.."); AsyncMeth(); Console.WriteLine("主线程测试结束..");