0

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!

Prime
  • 2,410
  • 1
  • 20
  • 35
  • 1
    When you say freezes - what does the debugger tell you? What line of code is the point of the freeze? – tsells Jun 24 '13 at 05:04
  • @Geek It doesn't say anything, it's as if the program is running fine but the GUI is frozen, I Broke All and it seems to have stopped on an invoke that appends the text to my output richTextBox. The invoke is right at the end of the inside of my try. Just above my `ThrowIfCancellationRequested();` – Prime Jun 24 '13 at 05:12
  • 1
    Answer is you must not access your UI controls from your loop without invoking. – tsells Jun 24 '13 at 05:15
  • It seems to be fixed, thanks for the help! – Prime Jun 24 '13 at 05:47

0 Answers0