0

I have a Page_Loaded event in an UWP application (Microsoft.NETCore.UniversalWindowsPlatform v6.2.10). I have made it async void so I can call asynchronous methods:

private async void Page_Loaded(object sender, RoutedEventArgs e)

I tried to do this in above event

Task s1 = something.MyMethodAsync();
Task s2 = something.MyOtherMethodAsync();
await Task.WaitAll(s1, s2);

but am getting cannot await void error.

If I re-write it this way it's fine, but I lose some of the benefits of asynchronous calls

await something.MyMethodAsync();
await something.MyOtherMethodAsync();

Why is WaitAll not working?

Cfun
  • 8,442
  • 4
  • 30
  • 62
under
  • 2,519
  • 1
  • 21
  • 40
  • 1
    `WaitAll` is blocking call – Pavel Anikhouski May 21 '20 at 08:56
  • Task.Waitall() function's return type is void. Hence u cannot perform anything on it. Similarly u can not await also resulting in error. Check the [definition](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.waitall?view=dotnet-uwp-10.0#definition) – neelesh bodgal May 21 '20 at 09:50

1 Answers1

0

Task.WaitAll is a blocking call. You want Task.WhenAll.

await Task.WhenAll(t1, t2);
Josh
  • 289
  • 3
  • 10
  • Also as a side note, you should avoid having an async void function https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.waitall?view=netcore-3.1 – Josh May 21 '20 at 08:39