I am opening n
concurrent threads in my function:
List _files = new List();
public void Start()
{
Cancellation
To get the behavior you want you need to write a custom partitioner, The reason it looks "random" is right now it is batching out the file list in blocks so if your source list was
List files = List { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
when it partitions it it may split it evenly like so (if Max was 3 threads):
So if you watched the files being processed it may look like
"a", "d", "g", "e", "b", "h", "c", "f", "i"
If you make a custom partitioner you can have it take one item at a time instead of a batch at a time to make the work list look like
GetTheNextUnprocessedString()
GetTheNextUnprocessedString()
GetTheNextUnprocessedString()
If you are using .NET 4.5 you can use this factory like so:
Parallel.ForEach(Partitioner.Create(_files, EnumerablePartitionerOptions.NoBuffering),
new ParallelOptions
{
MaxDegreeOfParallelism = 5 //limit number of parallel threads
},
(file, loopstate, index) =>
{
if (token.IsCancellationRequested)
return;
//do work...
});
If you are not using .NET 4.5, it is not a trivial task so I am not going to write it here for you. Read the MSDN article I linked at the top and you will be able to figure it out eventually.
What I would do is ask yourself "do I really need the files to be processed in order?" if you don't need them to be in order let it do its own ordering as the only thing you will likely do by enforcing a order is potentially slowing down the process.