Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

vb.net - Cross-thread operation not valid

I was doing the following to try out threadings but get the 'Cross-Threading' error in the line txtBox.Text = DataGridView2.Rows.Item(iloop).Cells(3).Value, anyone can point out what's wrong, thanks:

Dim lm As New Thread(AddressOf load_movie)
        Dim lt As New Thread(AddressOf load_timings)
        lm.Start()
        lt.Start()

Private Sub load_movie()

        For iloop As Integer = 0 To DataGridView1.Rows.Count - 2
            Dim Cstring As String = "txt_Movie_0" & iloop.ToString
            For Each cCtrl As Control In Panel1.Controls
                If TypeOf cCtrl Is TextBox Then
                    Dim txtBox As TextBox
                    txtBox = cCtrl
                    If txtBox.Name = Cstring Then
                        txtBox.Text = DataGridView1.Rows.Item(iloop).Cells(1).Value
                    End If
                End If
            Next
        Next
    End Sub

    Private Sub load_timings()
        For iloop As Integer = 0 To DataGridView2.Rows.Count - 2
            For Each cCtrl As Control In Panel2.Controls
                If TypeOf cCtrl Is TextBox Then
                    Dim txtBox As TextBox
                    txtBox = cCtrl
                    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 If
            Next
        Next

    End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...