C
C#2y ago
Kroks

✅ 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
Kroks
KroksOP2y ago
or better: I want to have some kind of callback for Task.WhenAll, when some item is awaited So like this:
protected async Task ExecuteTasks(int start, int end, Func<int, Task<V>> createTask)
{
var l = new Queue<Task<V>>();

for (int i = start; i < end; i++)
l.Enqueue(createTask(i));

var result = await Task.WhenAll(l);
}

protected async Task HandleAwaitedItem(V item)
{

}
protected async Task ExecuteTasks(int start, int end, Func<int, Task<V>> createTask)
{
var l = new Queue<Task<V>>();

for (int i = start; i < end; i++)
l.Enqueue(createTask(i));

var result = await Task.WhenAll(l);
}

protected async Task HandleAwaitedItem(V item)
{

}
I dont want to re-iterate over all items since its pretty huge and doing it multiple times.
canton7
canton72y ago
"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:
protected async Task ExecuteTasks(int start, int end, Func<int, Task<V>> createTask)
{
var l = new List<Task<V>>(end - start);

for (int i = start; i < end; i++)
{
l.Add(ExecuteTask(createTask(i)));
}

await Task.WhenAll(l);
}

private async Task ExecuteTask(Task<V> task)
{
var result = await task;
await HandleAwaitedItem(result);
}

protected async Task HandleAwaitedItem(V item)
{

}
protected async Task ExecuteTasks(int start, int end, Func<int, Task<V>> createTask)
{
var l = new List<Task<V>>(end - start);

for (int i = start; i < end; i++)
{
l.Add(ExecuteTask(createTask(i)));
}

await Task.WhenAll(l);
}

private async Task ExecuteTask(Task<V> task)
{
var result = await task;
await HandleAwaitedItem(result);
}

protected async Task HandleAwaitedItem(V item)
{

}
Kroks
KroksOP2y ago
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
canton7
canton72y ago
Once each task is completed? So like my code snippet above?
Kroks
KroksOP2y ago
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
canton7
canton72y ago
No it does not
Kroks
KroksOP2y ago
you await the task
canton7
canton72y ago
...yes? Try it. Your understanding of await is flawed
Kroks
KroksOP2y ago
You are correct, this should work. My bad dont work too often with async thanks for the help
canton7
canton72y ago
No worries!
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server