I need some advice on multithreading.
Example: I want to spawn 200 Threads, each one should grab one different image from an website (the site is kinda slow). But there should be max. 40 Threads at once.
Any idea?
I look forward to some answers.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can easily use
ThreadPoolfor this. The pool does a good job of managing threads but if you really really must limit the maximum number you can use:Unfortunately, there is no built-in way to wait for the tasks to finish so you’ll just have to improvise something. If you have access to .NET 4.0 you can check out
Parallel.For: