It seems my Parallel.ForEach loop freezes on it's last loop. I'm trying to loop through a string array and this is my syntax,
String [] split = checkerInput.Text.Split('\n');
int threads = Properties.Settings.Default.Threads;
int chunkSize = (split.Count() - 1 + threads - 1) / threads;
ParallelOptions pOptions = new ParallelOptions
{
MaxDegreeOfParallelism = threads,
CancellationToken = cts.Token
};
Parallel.ForEach(Partitioner.Create(1, split.Count() + 1, chunkSize), line =>
{
for (int i = line.Item1; i < line.Item2; i++)
{
try
{
string str = split[i - 1];
//...String stuff...
pOptions.CancellationToken.ThrowIfCancellationRequested();
}
catch (OperationCanceledException)
{
//...Stop stuff...
}
}
}
I increase split.Count() by one in the Partitioner because it seemed to miss out the last line of the array and I minus i by one so that there's an offset (Since Line 1 would be split[0], not split[1]).
It's going all fine until it reaches the last line of the array and just decides to freak out and freeze with no error. It's weird because I have another Parallel.ForEach loop that works just fine, only it loops x amount of times and not x amount of lines in a string array.
Any help would be much appreciated!