What's best practice when creating multiple tasks for Task.WhenAll()
?
Is there a difference between these two async-await alternatives below in terms of performance, and/or behavior under an exception?
They both work, but is there any difference?
Alternative 1:
private async void Button_Click(object sender, RoutedEventArgs e)
{
// call DoWork() to prepare data
await Task.WhenAll(myObjects.Select(o => Task.Run(() => o.DoWork()));
// update ui ...
}
Alternative 2:
private async void Button_Click(object sender, RoutedEventArgs e)
{
// call DoWork () to prepare data - note the async-await around the return Task
await Task.WhenAll(myObjects.Select(async o => await Task.Run(() => o.DoWork()));
// update ui...
}
Thank you.