2017-07-28 175 views
0
static void Main(string[] args) 
{ print(); } 

static async void print() 
{ 
    try 
    { 
     await Task.Factory.StartNew(() => 
     { 
      Thread.Sleep(3000); 
      Console.WriteLine("3"); 
      Debug.Write("3"); 
     }); 
    } 
    catch (Exception) 
    { } 
    Console.Read(); 
} 

控制台溅起来没有发生任何错误!C#正在等待任务控制台启动

回答

0

这是因为print方法并行调用所以main继续,因为没有别的事可做,所以它返回。在main完成程序退出后。

如果你想让它等待print方法,改变它,所以它会返回Task代替void,然后等待该任务在Main方法:

static void Main(string[] args) 
{ print().Wait(); } 

static async Task print() {...}