The Eng
The Eng

Reputation: 39

Debugging very slow.. What's wrong?

I'm writing a VB.net software. In the main form there are several Datagridviews and Listboxes which I'm filling with some parameters and plotting them into XY charts. At the end of the generation of all charts, I have to write, adding some premade strings too, some points of these Datagridviews/Listboxes on a RichtextBox that is on another form. So I'm showing the form and appending the text on the Richtextbox for the numbers I need. My issue is that this operation, that seems to be very simple, is very slow (about 5 minutes for the code part I'm reporting below) and often I receive a ContextSwitchDeadlock error (that I've switched off from the Debug settings of VS2019). Here is the code:

Form2.Show()
        Form2.RichTextBox6.AppendText("MPTEMP,,,,,,,,")
        If CDbl(TextBox1.Text) >= 55 Then
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,1," + DataGridView9.Rows(0).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,2," + DataGridView9.Rows(80).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,3," + DataGridView9.Rows(180).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,4," + DataGridView9.Rows(280).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,5," + DataGridView9.Rows(380).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,6," + DataGridView9.Rows(480).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,7," + DataGridView9.Rows(580).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(0).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(80).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(180).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(280).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(380).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(480).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(580).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
        Else
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,1," + DataGridView9.Rows(0).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,2," + DataGridView9.Rows(80).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,3," + DataGridView9.Rows(180).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPTEMP,4," + DataGridView9.Rows(280).Cells(0).Value.ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(0).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(80).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(180).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,EX,1,," + (DataGridView9.Rows(280).Cells(1).Value * 1000000).ToString())
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
            Form2.RichTextBox6.AppendText(Environment.NewLine + "MPDATA,PRXY,1,," + TextBox6.Text)
        End If
        Form2.RichTextBox6.AppendText(Environment.NewLine + "TB,MELA,1,2,3,")
        Return

Obvioulsy, when pressing the button which starts the code I've reported, the Datagridview values have been calculated yet, so it has simply to write those strings in the Richtextbox. Where am I doing something not efficient? How could I get it faster? Thanks all are gonna answer me. Best regards

Upvotes: 0

Views: 46

Answers (1)

Mary
Mary

Reputation: 15091

Every time you call .AppendText you are updating the user interface. This is a slow process. Build the string, then update the RichTextBox once.

To further speed things up use a StringBuilder. A String is immutable (you can't change it) so, if your just use a plain String variable, it will be thrown away and recreated for every addition. This is not so with a StringBuilder.

The StringBuilder is in the System.Text namespace so add

Imports System.Text

to the top of the code file.

If you have a DataTable bound to the grid, it might be easier to retrieve values there

dt(80)(1)

Where dt is the DataTable, 80 is the row, and 1 is column. Both row and column are zero based.

To make your code more readable, you can use interpolated strings in Visual Studio 2015 and later. They begin with a $. This allows you to include variables directly inline surrounded by braces { }. ToString is not necessary, it is provided by the interpolator.

Private Sub OpCode()
    Dim sb As New StringBuilder
    sb.AppendLine("MPTEMP,,,,,,,,")
    If CDbl(TextBox1.Text) >= 55 Then
        sb.AppendLine($"MPTEMP,1,{DataGridView9.Rows(0).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,2,{DataGridView9.Rows(80).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,3,{DataGridView9.Rows(180).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,4,{DataGridView9.Rows(280).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,5,{DataGridView9.Rows(380).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,6,{DataGridView9.Rows(480).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,7,{DataGridView9.Rows(580).Cells(0).Value}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(0).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(80).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(180).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(280).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(380).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(480).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{(DataGridView9.Rows(580).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
    Else
        sb.AppendLine($"MPTEMP,1,{ DataGridView9.Rows(0).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,2,{ DataGridView9.Rows(80).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,3,{ DataGridView9.Rows(180).Cells(0).Value}")
        sb.AppendLine($"MPTEMP,4,{ DataGridView9.Rows(280).Cells(0).Value}")
        sb.AppendLine($"MPDATA,EX,1,,{ (DataGridView9.Rows(0).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{ (DataGridView9.Rows(80).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{ (DataGridView9.Rows(180).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,EX,1,,{ (DataGridView9.Rows(280).Cells(1).Value * 1000000)}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
        sb.AppendLine($"MPDATA,PRXY,1,,{ TextBox6.Text}")
    End If
    sb.Append("TB,MELA,1,2,3,")
    Form2.RichTextBox6.Text = sb.ToString
    Form2.Show()
   
End Sub

Upvotes: 1

Related Questions