I am trying to understand how the code below would behave differently if the functions used in Task.Run were not async/await when calling GetAllData.
Am I correct that GetAllData will behave the same regardless of what is used with Task.Run? Meaning that it will block until all of the tasks are completed? If that is true, how does the flow change when removing the async/await from Task.Run, assuming GetData1/GetData2 are long running CPU-bound methods?
public Task GetAllData()
{
Task.WaitAll(
Task.Run(async () => await SomeFunction(() => GetData1)),
Task.Run(async () => await SomeFunction(() => GetData2))
//versus
//Task.Run(() => SomeFunction(() => GetData1)),
//Task.Run(() => SomeFunction(() => GetData2))
);
Debug.Writeline("All tasks completed");
return Task.CompletedTask;
}
async Task SomeFunction(Action action)
{
//do some work.
await action.TimeoutAfter(TimeSpan.FromMinutes(2));
}
async Task TimeoutAfter(this Action action, TimeSpan timeout)
{
var task = Task.Run(action);
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
{
await task;
}
else
{
//timeout logic
}
}