Async modifier in C#

前端 未结 3 458
逝去的感伤
逝去的感伤 2021-01-01 15:10

I have the question, what is the difference between these two methods?

    async private void Button_Click_1(object sender, RoutedEventArgs e)
    {
                 


        
3条回答
  •  余生分开走
    2021-01-01 15:22

    Adding async, by itself, does nothing other than allow the method body to use the await keyword. A properly implemented async method won't block the UI thread, but an improperly implemented one most certainly can.

    What you probably wanted to do was this:

    async private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        await Task.Delay(2000);
        MessageBox.Show("All done!");
    }
    

提交回复
热议问题