0

We had a test case which had earlier below snippet written but the test case was failing due to race condition.

Task.WaitAll(result1, result2);

But when it changed to below it started working fine.

await result1; await result2;

Note: also we marked Test case as Async too but I don't think it makes any difference since I checked all test run async alphabetically anyway by Microsoft Unit Test framework.

abksharma
  • 576
  • 7
  • 26
  • 2
    Have you tried using `await Task.WhenAll(...)` instead of `Task.WaitAll(...)`. [See other SO question](https://stackoverflow.com/questions/6123406/waitall-vs-whenall) – Martin Aug 04 '22 at 10:15
  • 1
    Also keep the exception handling of `WhenAll()` in mind ([see here](https://alt-dev.com.au/why-you-may-be-doing-async-wrong-in-c#heading-waitall-waits-for-all-tasks-to-complete)) – mu88 Aug 04 '22 at 10:58

1 Answers1

1

Task.WaitAll(result1, result2); is Blocking Call. it means that your current thread is blocked until your task is Done.

await is a non-blocking call: it means that your current Thread will not blocked.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await

multiple awaits vs Task.WaitAll - equivalent?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92