Let's say that what I want to do is to validate one million strings, and each validation takes a couple of seconds.
My approach:
I have an array of threads declared like this:
Thread[] workers = new Thread[50];
I don't have all strings in an array, they are got through some calculations, then I don't have all of them when I start the process, but I have a method that returns the next one:
public string next()
{
//my code
}
I've been able to run all the 50 threads like this:
for (int x = 0; x < 50; x++)
{
workers[x] = new Thread(new ParameterizedThreadStart(myMethod));
workers[x].Start(next());
}
Which swiftly starts all 50 threads "at the same time" and then my log (fed by myMethod) gets 50 responses almost at the same time (1~1.5 second)
How do I get every thread that has just completed to run again with the next string taking into account that the Thread class doesn't expose any event or anything similar ?
Note: I have done some performance tests, and I prefer to use the regular Threads rather than BackgroundWorkers.
Using C# in .net 3.5.