C# async等待和不等待
代码:
class Program { private static void Main(string[] args) { Console.WriteLine("start0"); var tt = CountCharactersAsync(null); Console.WriteLine("end0"); Console.WriteLine("start1"); var t = CountCharactersAsync(null); t.Wait(); //等待任务结束 Console.WriteLine("end1"); Console.Read(); } private static async Task<int> CountCharactersAsync(string address) { Console.WriteLine("CountCharactersAsync-start"); if (string.IsNullOrEmpty(address)) { await Task.Run(() => Thread.Sleep(2000)); Console.WriteLine("CountCharactersAsync-end"); return 3000; } else { Console.WriteLine("CountCharactersAsync-end"); return 10; } } }