At the moment I have List of Job objects that are queued to be processed sequentially shown in the code bellow.
List<Job> jobList = jobQueue.GetJobsWithStatus(Status.New);
foreach (Job job in jobList)
{
job.Process();
}
I am interested in running several Jobs at the same time in a limited number of threads (lets say 5 threads).
What is the best way to do this in c#?
Additional Notes:
Update: I have used a Semaphore because I could not limit the amount of active threads with a ThreadPool.
If you're feeling adventurous you can use C# 4.0 and the Task Parallel Library:
Parallel.ForEach(jobList, curJob => {
curJob.Process()
});
You will want to look into thread pools (for the simple answer). There is even a ThreadPool class in C# and it is quite easy to set up with great examples in the msdn library.
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