parallel.for - what's wrong with him?



  • I have a job to create a textbox sign, a width. TableWidthheight TableHeigth♪ For large values (50*50), the plate shall be established for a long time, so decided to make a parallel drawing. But this code weighs the program completely. Can you tell me what might be wrong? There are assumptions that there's a tampering operation going on somewhere, but where exactly... ♪

    Parallel.For(1, table.TableWidth + 1, x =>
    {
        var i = x;
        for (int y = 1; y <= table.TableHeight; y++)
        {
            var j = y;
            TextBox textbox;
            if (!textBoxes.ContainsKey(new Point(i, j)))
            {
                textbox = new TextBox();
                textBoxes.Add(new Point(i, j), textbox);
                textbox.BorderStyle = BorderStyle.Fixed3D;
                Controls.Add(textbox);
            }
    
        textbox = textBoxes[new Point(i, j)];
        textbox.Location = new Point(ColumnsCoords[i], RowsCoords[j]);
        textbox.Width = table.ColumnsWidth[i];
        textbox.Height = table.RowsHeight[j];
        textbox.Text = table[i, j].Data;
    
        textbox.GotFocus += (s, a) =&gt;
        {
            focusedCellCoords.Text = new Point(i, j).ToString();
            currentTextBox = textbox;
            focusedCell.Text = textbox.Text;
        };
        textbox.TextChanged += (s, a) =&gt;
        {
            PushData(new Point(i, j), textbox.Text);
            currentTextBox = textbox;
            focusedCell.Text = textbox.Text;
        };
    }
    

    });



  • Even if you hadn't done the code, he couldn't work. The thing is, any countermeasure listens to the flow that was created. But the communications cycle only rolls in the main flow of the application, the pools cannot receive final messages. Therefore, your textboxes will not respond to events (this cannot even introduce text, the introduction of the text also requires an active communication cycle).

    The only possible solution is to throw away Parallel.For and replace it with the normal cycle for


    Well, prior to the suspension of the program, I'll risk assuming that there's an interlock in the line. Controls.Add(textbox); - I'm sure this method has to send an end message to the main UI flow, and it's just running the cycle. for♪ But it's a hydration, if you need a precision, you're gonna have to run a snack and check where it hangs.

    Another mistake you might have made - judging by code, textBoxes is a simple dictionary.Dictionary<Point,TextBox>) But you can't work with a dictionary at the same time! If the code had any meaning, you should use it. ConcurrentDictionary<,> for the same purpose.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2