跨线程操作在标签数组中无效

发布于 2024-11-16 08:05:59 字数 1029 浏览 3 评论 0原文

我有一个问题:当尝试更改标签数组的文本 (label(1).text = "Lol") 时,我收到错误:

"Cross-thread operation not valid: Control 'lblCSCH1' accessed from a thread other than the thread it was created on."

代码是这样的:

    Private Sub Cliente_Receive(ByRef message As String) Handles Cliente.Receive
    Dim anterior As String
    Dim corte As Integer
    Dim canal As String
    Dim lblCSCH() As Label = {lblCSCH0, lblCSCH1, lblCSCH2, lblCSCH3, lblCSCH4, lblCSCH5, lblCSCH6, lblCSCH7, lblCSCH8, lblCSCH9, lblCSCH10}

    If Microsoft.VisualBasic.Left(message, 3) = "<ch" Then
        corte = InStr(message, ">")
        If corte > 0 Then
            corte = corte - 1
            canal = Replace(LTrim(Replace(Replace(Replace(Replace(Microsoft.VisualBasic.Left(message, corte), "h", ""), "c", ""), "<", ""), "0", " ")), " ", "0")
            'After this i Get just a number, for example 1 or 2
            lblCSCH(canal).Text = canal
        End If
    End If

End Sub

我该如何解决这个问题?

I have a problem: When try to change the text of a label array (label(1).text = "Lol") then I get a error:

"Cross-thread operation not valid: Control 'lblCSCH1' accessed from a thread other than the thread it was created on."

The code is this:

    Private Sub Cliente_Receive(ByRef message As String) Handles Cliente.Receive
    Dim anterior As String
    Dim corte As Integer
    Dim canal As String
    Dim lblCSCH() As Label = {lblCSCH0, lblCSCH1, lblCSCH2, lblCSCH3, lblCSCH4, lblCSCH5, lblCSCH6, lblCSCH7, lblCSCH8, lblCSCH9, lblCSCH10}

    If Microsoft.VisualBasic.Left(message, 3) = "<ch" Then
        corte = InStr(message, ">")
        If corte > 0 Then
            corte = corte - 1
            canal = Replace(LTrim(Replace(Replace(Replace(Replace(Microsoft.VisualBasic.Left(message, corte), "h", ""), "c", ""), "<", ""), "0", " ")), " ", "0")
            'After this i Get just a number, for example 1 or 2
            lblCSCH(canal).Text = canal
        End If
    End If

End Sub

How can I resolve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空名 2024-11-23 08:05:59

Cliente_Receive 事件发生在后台线程上。您需要使用 Control.Invoke 将回调编组回 UI 线程。

您需要做的唯一更改是将其更改

'After this i Get just a number, for example 1 or 2
lblCSCH(canal).Text = canal

为:

'After this i Get just a number, for example 1 or 2
lblCHCH(canal).Invoke(Sub() lblCSCH(canal).Text = canal)

The Cliente_Receive event is occurring on a background thread. You need to use Control.Invoke to marshal the call back to the UI thread.

The only change you need to do is to change this:

'After this i Get just a number, for example 1 or 2
lblCSCH(canal).Text = canal

To:

'After this i Get just a number, for example 1 or 2
lblCHCH(canal).Invoke(Sub() lblCSCH(canal).Text = canal)
颜漓半夏 2024-11-23 08:05:59

在代码的表单加载部分尝试此操作。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Control.CheckForIllegalCrossThreadCalls = False
'Rest of load form data
End Sub

Try this in the form load section of your code.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Control.CheckForIllegalCrossThreadCalls = False
'Rest of load form data
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文