C
C#3y ago
jin.fuyou

get value from parallel for loop using Task.WhenAll

hi all, im trying to implement a parallel for loop because of some timeout issue (the normal loop will loop through large dataset and it's taking lots of time so i try to use Task.WhenAll), im trying to process all items inside this await Task.WhenAll(tasks); line but im wondering how do we get the value returned by ProcessItem method because we put them inside the WhenAll to process, can anyone help to advise? thank you so much in advance..
IList<Note> resources = new List<Note>();
var tasks = new List<Task>();
foreach (var item in result)
{
tasks.Add(ProcessItem(item));
}
await Task.WhenAll(tasks); //get the value from here and initialize into the resources variable

NoteList noteList = new NoteList ()
{
Resources = resources
};
return noteList;

IList<Note> resources = new List<Note>();
var tasks = new List<Task>();
foreach (var item in result)
{
tasks.Add(ProcessItem(item));
}
await Task.WhenAll(tasks); //get the value from here and initialize into the resources variable

NoteList noteList = new NoteList ()
{
Resources = resources
};
return noteList;

private async Task<IList<Note>> ProcessItem(NoteItem item)
{
IList<Note> resources = new List<Note>();
var note= await getNote(item);
Note resource = _mapper.Map<Note>(note);
resources.Add(resource);
return resources;
}
private async Task<IList<Note>> ProcessItem(NoteItem item)
{
IList<Note> resources = new List<Note>();
var note= await getNote(item);
Note resource = _mapper.Map<Note>(note);
resources.Add(resource);
return resources;
}
3 Replies
jin.fuyou
jin.fuyouOP3y ago
it's only one item i need to retrieve that's a List, do i have to implement another for loop..? no, i want the completed result from the ProcessItem method
jin.fuyou
jin.fuyouOP3y ago
jin.fuyou
jin.fuyouOP3y ago
umm. what do you mean? so how do we get the result from Task.WhenAll..? is it to implement normal for loop and add the result from Task.WhenAll into another list? i see.. is it possible if we maintain the result as a IList type instead of array type? so like we only initialize it directly IList<Note> resources = await Task.WhenAll(tasks); i see.. hmm.. okay can.. i'll try it out.. thank you!! 🙏 wait.. i just realized this is not ok.. Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.List<Core.Models.Note.Note>>' to 'System.Collections.Generic.List<Core.Models.Note.Note>' it becomes List of a List if we do var tasks = new List<Task<IList<Note>>>(); but our expected result is only a List we have to loop through the tasks to add into List<Note> resources = new List<Note>();?
List<Note> resources = new List<Note>();
var tasks = new List<Task<List<Note>>();
foreach (var item in result)
{
tasks.Add(ProcessItem(item));
}
resources = (await Task.WhenAll(tasks)).ToList(); //there's an extension so i try to convert from array to list
List<Note> resources = new List<Note>();
var tasks = new List<Task<List<Note>>();
foreach (var item in result)
{
tasks.Add(ProcessItem(item));
}
resources = (await Task.WhenAll(tasks)).ToList(); //there's an extension so i try to convert from array to list
private async Task<List<Note>> ProcessItem(NoteItem item)
{
List<Note> resources = new List<Note>();
var note= await getNote(item);
Note resource = _mapper.Map<Note>(note);
resources.Add(resource);
return resources;
}
private async Task<List<Note>> ProcessItem(NoteItem item)
{
List<Note> resources = new List<Note>();
var note= await getNote(item);
Note resource = _mapper.Map<Note>(note);
resources.Add(resource);
return resources;
}
sry i edited the code, bcs the extension only support List instead of IList.. resources = (await Task.WhenAll(tasks)).ToList(); //this line ohhhh ohhh.. i try this out... it's working, thank you for your help! have a great day!!
Want results from more Discord servers?
Add your server