Click here to Skip to main content
15,888,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've got 4000 things to do in a loop, and I'd like to do some of them at the same time. I believe Parallel.For scales to the number of cores which on our server is 48. I don't want that many running at the same time as they open network connections etc. and I have a feeling things couldn't handle it downstream so well.

How would you do this? Say up to five things at once? As simply as possible?
Posted

you could set the MaxDegreesOfParallelism[^] option in your Parrallel.For loop.

Alternative options I could suggest is look at the Threadpool Class as you are also able to limit the number of threads that it manages.

ThreadPool.SetMaxThreads[^]
 
Share this answer
 
Comments
Rob Philpott 12-Aug-14 11:07am    
Ah, yes, I should look at the documentation more closely. That is it, perfectly! Thanks.

I think setting maximum threads on the threadpool might cause the sky to fall in, because that's for your whole process.
I'd divide the 4000 objects into five lists of objects, and hand each list to a separate BackgroundWorker to process.
 
Share this answer
 
Comments
Rob Philpott 12-Aug-14 11:05am    
hmmm. Could do. Not very simple/neat though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900