C
C#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.
K
Kroks384d 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.
C
canton7384d 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)
{

}
K
Kroks384d 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
C
canton7384d ago
Once each task is completed? So like my code snippet above?
K
Kroks384d 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
C
canton7384d ago
No it does not
K
Kroks384d ago
you await the task
C
canton7384d ago
...yes? Try it. Your understanding of await is flawed
K
Kroks384d ago
You are correct, this should work. My bad dont work too often with async thanks for the help
C
canton7384d ago
No worries!
A
Accord383d 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
More Posts
❔ How to get azure cache for Redis connection string stored in app service as a appsetting variableI have created a azure redis for cache and stored the connection string as application setting varia❔ Giveaway for an online DOTNET ConferenceHello developer students, I am going to give away free tickets to an online dotnet conference whic❔ SoundBoardHi! I am working on a soundboard lately and i got stuck with playing a sound throught microphone. i'❔ accessing object from a class inside of another classI got this class called "Engine"(image 1) that has the object "isOn" which indicates whether the eng❔ QuestionControllerHello, i have a question regarding API structure. I have two classses, Item and Store. I created C❔ Problem Binding to Image SizeI am making an app that displays a series of books. An image and title of the book is to be displaye❔ whats the best site or youtube channel to start learning c#?just downloaded visual studio and wanted to know what would be the best way to learn C#❔ C# MVC - Erroring in VSC not VS2022I'm writing a C# .Net Core 7.0 web application with dapper for my SQL queries. It runs just fine inString Contain issueSo im trying to get a word in a string using Contain but its not getting the word, I havent found a ❔ Catching Exceptions with generic parameters?When adding an entity to the database, i first check if it already exists and if it does I throw an ❔ How should I learn programming?I'm sure this is a common question, but I want to learn game development but am a pretty new C# prog❔ How to properly structure projects in a multi-app MVVM solution?I tend to structure my MVVM projects in the following way: ``` . ├── src/ │ ├── Applications/ │ Sending File and Data to a Minimal API endpointHi. I am using .NET 7, Minimal API and Mailkit. What I want to do is to send an attachment along wi❔ reprotucing the kings pathcan anybody help me fix this code https://paste.mod.gg/sgsvrbaqnmpi/0 ``` the input is number of obs❔ Its there a way to fix this?Hi there, currently I am trying to develop some functions to publish them to azure later, but right ✅ Does this styling code look right?I'm using this code for a simple hover effect. Im wondering if I'm doing this correctly: <Window.Re❔ ?[] operator for dictionariesIs not working. It is explained here. https://learn.microsoft.com/en-us/dotnet/csharp/language-refer❔ IAsyncEnumerable vs List<T> as parameter in methodI have a method that is overloaded and will take in a List<T> or IAsyncEnumerable. The method will b❔ This code is bugged how can i solve it?```LogiwaEntities1 db = new LogiwaEntities1(); ProductData productData = new ProductData✅ How to add hover effect to WPF button elementThis code I'm using isn't working. When I hover over buttons its a lightblue color. <Style