在C#中使用异步 async await
1.如果您的vs中显示 Main 方法报错,请在项目设置中将您的C#版本改为默认最新或直接选择C#7.0
1 class Program 2 { 3 public static async Task Main(string[] args) 4 { 5 var result = await Save(); 6 Console.WriteLine(result); 7 Console.ReadLine(); 8 9 } 10 11 public static async Task<string> Save() 12 { 13 var wc = new WebClient(); 14 var result = await wc.DownloadStringTaskAsync("https://www.baidu.com/"); 15 await Task.Run(() => 16 { 17 File.WriteAllText("D:/reuslt.txt", result); 18 }); 19 return "Success write"; 20 21 } 22 23 }
笔记持续跟新中(如果看到本作者文档中无此字眼说明该文档已停止跟新) ......