加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – 跨线程操作无效

发布时间:2020-12-17 00:13:36 所属栏目:大数据 来源:网络整理
导读:我正在做以下尝试线程,但在行txtBox.Text = DataGridView2.Rows.Item(iloop).Cells(3).Value,得到’交叉线程’错误,任何人都可以指出什么是错的,谢谢: Dim lm As New Thread(AddressOf load_movie) Dim lt As New Thread(AddressOf load_timings) lm.Start(
我正在做以下尝试线程,但在行txtBox.Text = DataGridView2.Rows.Item(iloop).Cells(3).Value,得到’交叉线程’错误,任何人都可以指出什么是错的,谢谢:
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
@JaredPar你有基本的想法,但代码本身不会编译(除非我遗漏了一些东西).对于VB9或更低版本,您需要声明一个实际的委托并调用:
''//The delegate is only needed for the VB 9 or less version
    Private Delegate Sub UpdateTextBoxDelegate(ByVal txtBox As TextBox,ByVal value As String)

    If TypeOf cCtrl Is TextBox Then
        Dim txtBox As TextBox
        txtBox = cCtrl
        ''//Perform validation logic here
        If (txtBox.Name.Substring(9,6)) = (DataGridView2.Rows.Item(iloop).Cells(0).Value.ToString().Substring(0,6)) Then
            ''//Call the update method with our textbox and value
            UpdateTextBox(txtBox,DataGridView2.Rows.Item(iloop).Cells(3).Value.ToString())
        End If
    End If

    Private Sub UpdateTextBox(ByVal txtBox As TextBox,ByVal value As String)
        ''//Basically ask the textbox if we need to invoke
        If txtBox.InvokeRequired Then
            ''//For VB 9 or less you need a delegate
            txtBox.Invoke(New UpdateTextBoxDelegate(AddressOf UpdateTextBox),txtBox,value)
        Else
            txtBox.Text = value
        End If
    End Sub

对于VB 10,我们最终可以使用匿名子,因此我们可以完全摆脱委托:

If TypeOf cCtrl Is TextBox Then
        Dim txtBox As TextBox
        txtBox = cCtrl
        ''//Perform validation logic here
        If (txtBox.Name.Substring(9,DataGridView2.Rows.Item(iloop).Cells(3).Value.ToString())
        End If
    End If

Private Sub UpdateTextBox(ByVal txtBox As TextBox,ByVal value As String)
    If txtBox.InvokeRequired Then
        ''//For VB 10 you can use an anonymous sub
        txtBox.Invoke(Sub() UpdateTextBox(txtBox,value))
    Else
        txtBox.Text = value
    End If
End Sub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读