Let’s say I have a
List<Thread> df = new List<Thread>();
// I add 500 instances of delegate() { somemethod(a,b) }; to df
Now I want to run through all df items threading it X items maximum at a time, how can I accomplish this?
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.
There are a variety of options at your disposal. Depending on what framework you have access to, the options may differ. At your disposal through .NET 4, you have: Thread class w/ Signals, ThreadPool, Asynchronous Delegate Invocation, Task Parallel Library.
I’ll run through a couple of your options here, and you can pick the one that best fits your needs.
Threading and Signaling
The first option is the most manual. You would spin up instances of the Thread class up to your threshold X, and whenever a thread completes, spin up another thread.
Example
ThreadPool
As an alternative to using a List, you can queue worker items in the ThreadPool, and let it work through them at its leisure. Be wary of using the ThreadPool to process a large amount of concurrent work. There is ONE thread pool per AppDomain, which defaults to about 25 concurrent threads. Those threads must be available to handle ALL threads in that app domain, including things the runtime may do in the background. ThreadPool should only be used for a small amount of concurrent work items.
Example
Asynchronous Delegate Invocation
Since you are using delegates, it is possible to use asynchronous invocation to implement threading. This is a more abstracted approach to multithreading, and sometimes easier to manage. In this case, it is very similar to starting your own threads manually. It should be noted that Delegate.BeginInvoke uses the ThreadPool internally, and the same rules that apply to using ThreadPool itself apply here as well.
Example
.NET 4 Task Parallel Library
If you have access to .NET 4, you could also try using the Task Parallel Library. Task parallelism is a newer approach to multithreading in .NET 4 that offers simple use and very rich runtime debugging capabilities.
In the example below, the
TaskCreationOptions.PreferFairnesstells the task parallel library to attempt to let tasks started earlier complete earlier, resulting in generally sequential execution of tasks.Example
Disclaimer: These examples are off the top of my head, and explicitly simplistic, so I make no guarantees about their perfect stability in a real-world scenario. I recommend using these as a basis to implement threading that is appropriate for whatever application you apply them to.