It's not legal to access a UI element from anything other than UI thread in .Net code. Hence when you try and use the DataGridView2
instance from a background thread it rightfully throws an exception.
In order to read or write to a UI component you need to use Invoke
or BeginInvoke
method to get back on the UI thread and make the update. For example
If TypeOf cCtrl Is TextBox Then
Dim txtBox As TextBox
txtBox = cCtrl
txtBox.Invoke(AddressOf UpdateTextBox, txtBox, iloop)
End If
Private Sub UpdateTextBox(txtBox as TextBox, iloop as Integer)
If (txtBox.Name.Substring(9, 6)) = (DataGridView2.Rows.Item(iloop).Cells(0).Value.substring(0, 6)) Then
txtBox.Text = DataGridView2.Rows.Item(iloop).Cells(3).Value 'This is the part that says "Cross-thread operation not valid: Control 'txt_Time_00_000' accessed from a thread other than the thread it was created on."
End If
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…