Reputation: 16320
I use 2 Parallel.ForEach
nested loops to retrieve information quickly from a url. This is the code:
while (searches.Count > 0)
{
Parallel.ForEach(searches, (search, loopState) =>
{
Parallel.ForEach(search.items, item =>
{
RetrieveInfo(item);
}
);
}
);
}
The outer ForEach
has a list of, for example 10, whilst the inner ForEach
has a list of 5. This means that I'm going to query the url 50 times, however I query it 5 times simultaneously (inner ForEach
).
I need to add a delay for the inner loop so that after it queries the url, it waits for x seconds - the time taken for the inner loop to complete the 5 requests.
Using Thread.Sleep
is not a good idea because it will block the complete thread and possibly the other parallel tasks.
Is there an alternative that might work?
Upvotes: 0
Views: 2743
Reputation: 7395
To my understanding, you have 50 tasks and you wish to process 5 of them at a time.
If so, you should look into ParallelOptions.MaxDegreeOfParallelism to process 50 tasks with a maximum degree of parallelism at 5. When one task stops, another task is permitted to start.
If you wish to have tasks processed in chunks of five, followed by another chunk of five (as in, you wish to process chunks in serial), then you would want code similar to
for(...)
{
Parallel.ForEach(
[paralleloptions,]
set of 5, action
)
}
Upvotes: 4