I want to make calls to a service which gives some json result. Depending on a few conditions i have to save the response in different objects. When i use a foreach loop it works fine but it takes a lot of time. Same code when i use a parallel foreach loop it skips some part of the result. I have tried using task.WaitAll(). Any suggestions? P.S. First question on stack overflow.
            Asked
            
        
        
            Active
            
        
            Viewed 62 times
        
    -2
            
            
        - 
                    1welcome to stackoverflow. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Jul 22 '21 at 17:02
1 Answers
0
            
            
        I am not sure you are using correctly the Parallel function as you didn't write here any code.
I think that the solution to it is using Parallel.ForEach and you might also update its ParallelOptions and specify the sound of maximum parallelism you wish.
I think you probably didn't use the WhenAll method together with the parallel ForEach which might be the cause for the problems you faced.
It might be something like:
List<Task> tasks = new();
Parallel.ForEach(<source>, new ParallelOptions
   { MaxDegreeOfParallelism = <MAX_DEGREE_OF_PARALLELISM> }, s =>
{
    tasks.Add(<action>);
});
await Task.WhenAll(tasks);
Note, in order to make the script above work fine you need to provide: source, MAX_DEGREE_OF_PARALLELISM, action.
 
    
    
        Misha Zaslavsky
        
- 8,414
- 11
- 70
- 116
