Concurrently awaiting multiple asynchronous calls with independent continuations

前端 未结 4 959
时光说笑
时光说笑 2021-01-06 14:11

There are several scenarios where I need to invoke multiple asynchronous calls (from the same event handler) that can proceed independently of each other, with each one havi

4条回答
  •  时光说笑
    2021-01-06 14:22

    private async void button_Click(object sender, RoutedEventArgs)
    { 
        var nameTask = GetNameAsync();
        var cityTask= GetCityAsync();
        var rankTask= GetRankAsync();
    
        System.Threading.Tasks.Task.WaitAll(nameTask, cityTask, rankTask);
    
        nameTextBox.Text = nameTask.Result;
        cityTextBox.Text = cityTask.Result;
        rankTextBox.Text = rankTask.Result;
    }
    

    More details: https://msdn.microsoft.com/pt-br/library/dd270695(v=vs.110).aspx

提交回复
热议问题