0

How can I retry the failed Task. Below executes tasks asynchronously. If one or many tasks failed I would like to retry the failed tasks for 3 times before setting the task as failed.

await Task.WhenAll(tasks).ConfigureAwait(false);
if (!tasks.Any(a => a.Result == false))
{
   result = true;
}
else 
{
   // Retry
}
Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53
Nash
  • 525
  • 3
  • 6
  • 24
  • Have you thought about moving the retry logic into the task itself? As it stands you are waiting for every task to complete, then looking for false, then planning to try again, which could extend overall run time. – Tyler Hundley Mar 05 '20 at 19:41
  • 1
    Does this answer your question? [How to use Task.WhenAny and implement retry](https://stackoverflow.com/questions/43763982/how-to-use-task-whenany-and-implement-retry) – devNull Mar 05 '20 at 19:43
  • 1
    @devNull Was just about to recommend [Polly](https://github.com/App-vNext/Polly#handing-return-values-and-policytresult) if the retry logic couldn't be moved into the task itself. Probably the cleanest way to implement this. – Tyler Hundley Mar 05 '20 at 19:46
  • I think Polly is a better option. – Nash Mar 05 '20 at 21:01
  • 1
    I just added a [new answer](https://stackoverflow.com/questions/43763982/how-to-use-task-whenany-and-implement-retry/60555163#60555163) to the duplicate question. – Theodor Zoulias Mar 05 '20 at 23:00

0 Answers0