My existing c# app updates a sharepoint site collection. Now I've been task to make my c# app update not 1 but 15 site collections. It would take too long to run sequentially, so threading would be a good solution.
However I can't just open 15 threads at once, I would like to have a configurable value which will be the max number of threads to run at once.
What I would expect is:
So my question is: Is all this build into .net threading. Or do I have to manually create the thread management code?
EDIT: this is a framework 3.5 project, sorry for not mentioning this before.
just use Tasks and don't think about the thread-numbers. The TPL will do all this for you.
The book C# in a Nutshell, 4.0, (by Albahari & Albahari) gives a neat example of simultaneously calling many "web clients" and downloading information. It uses PLINQ, does it in one or so lines of code. The PLINQ library handles the creation of the threads, etc. If you look at it, it doesn't tend to make too many threads, although you can force a limit of the maximum number of threads at a time. Similarly, you can use the Parallel.Foreach() and use the "ParallelOptions" parameter to limit the number of threads.
The nice thing is that you never have to create the threads yourself - it's automatic. And it does a fine job of load balancing.
A good tutorial is http://www.albahari.com/threading/ - look at Part 5 on Parallel Programming for lots of examples of using PLINQ and Parallel.Foreach.
Also, the Wagner book on Effective C# (4.0) has similar examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With