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?
The
Cliente_Receiveevent is occurring on a background thread. You need to useControl.Invoketo marshal the call back to the UI thread.The only change you need to do is to change this:
To: