Variable value increases +1 within Thread

Asked

Viewed 37 times

0

I am trying to change the color of the Datagrid line and this is easy. However, something is happening with the variable value idxRow2 when this is accessed in the new Thread.

The problem is in the function Begininvoke of the control of Datagrid.

Follows code:

        for (int idxRow2 = 0; idxRow2 < values.Length; idxRow2++)
        {
            if (values[idxRow2][3] == fName)
            {
                MessageBox.Show("Idx: " + idxRow2 + " | Name: " + fName);

                dataGridViewDownloader.BeginInvoke((Action)delegate () { dataGridViewDownloader.Rows[idxRow2].DefaultCellStyle.BackColor = Color.Red; dataGridViewDownloader.Update(); });
            }
        }

As seen above, I did a brief debug with the message, however, the variable idxRow2 says a value in the message and in the debug of the VS error the idxRow2 variable returns the value of the +1 loop.

Can anyone explain why this happens?

  • Begininvoke does not necessarily start a Thread at the moment it is called, maybe you can create a temporary variable tempIdxRow2, store the idxRow2 information before calling Begininvoke and, inside delegate, use this temporary variable(I have never tested this approach but perhaps it will solve).

  • Opa Guilher, tranquil? ...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.