我有一个问题:当尝试更改标签数组的文本( 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我该如何解决这个问题?
发布于 2011-06-21 02:04:52
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)发布于 2011-06-21 02:04:32
在代码的表单加载部分尝试这一点。
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 Subhttps://stackoverflow.com/questions/6415397
复制相似问题