I’m trying to create an AggregateException
to observe some behavior. I’m getting en error here: “Cannot await void.” I’m not sure why. Copilot is telling me this:
To fix the error “Cannot await ‘void'”, you need to change the return type of the
RunAsync
method fromvoid
toTask
.
As you can see, RunAsync
already has a return type of Task
. What am I missing?
namespace ThreadingPracticeTime
{
public class ThreadingPractice
{
public async Task RunAsync()
{
try
{
await Task.WaitAll(Method1Async(), Method2Async(), Method3Async());
}
catch (AggregateException ae)
{
foreach (var e in ae.InnerExceptions)
{
Console.WriteLine($"An aggregate error occurred: {e.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"A general error occurred: {ex.Message}");
}
Console.WriteLine("done");
}
private async Task Method1Async()
{
await Task.Delay(5000); // Example delay
throw new Exception("An error occurred in Method1Async");
}
private async Task Method2Async()
{
await Task.Delay(3000); // Example delay
}
private async Task Method3Async()
{
await Task.Delay(1000); // Example delay
throw new Exception("An error occurred in Method3Async");
}
}
}