✅ How to install a callback for an awaited item when using Task.WhenAll
Hello I am using Task.WhenAll on a List of Tasks. However if a task fails I want to immediately call another function on the task (for retrying it and handling other things). How can I do that?
I want a solution with good performance.
11 Replies
or better: I want to have some kind of callback for Task.WhenAll, when some item is awaited
So like this:
I dont want to re-iterate over all items since its pretty huge and doing it multiple times.
"Call another function on the task" -- what do you mean by that?
Task
doesn't have any other functions you can call
And in your second snippet, it's not clear when HandleAwaitedItem
should be called?
Do you want something like:
Not really, the tasks are doing webrequests that should not be done sequentially (wait for one request then do another) but rather more or less in parallel. I just want to trigger the HandleAwaitedItem once a task is finished
Once each task is completed? So like my code snippet above?
No your code snippet executes the tasks sequentially, it awaits one before it executes the next one. I want basically to use WhenAll but have the HandleAwaitedItem function triggered when a task completed
No it does not
you await the task
...yes?
Try it. Your understanding of await is flawed
You are correct, this should work. My bad
dont work too often with async
thanks for the help
No worries!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.