You are trying to modify a property of a control which resides on a different thread than the thread from which you call Rename()
. When updating controls you should check if it is required to send the call to the appropriate thread, or "invoke" the call. This should work.
Method 1: Always Invoke
Public Sub Rename(ByVal labelno As Integer)
Try
Dim lbl As New Label
lbl = Controls("Label" & labelno.ToString())
lbl.Invoke(Sub() lbl.Text = ReceivedFrame.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub
It will always invoke the call, which is unnecessarily costly when Rename()
is called from the control's thread. But it should always work. You can also do it this way to only invoke when required as not to make unnecessary invocations to the UI thread.
Method 2: Invoke only if required
Public Sub Rename(ByVal labelno As Integer)
Try
Dim lbl As New Label
lbl = Controls("Label" & labelno.ToString())
changeLabelText(lbl, ReceivedFrame.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub
Private Delegate Sub changeLabelDel(lbl As Label, txt As String)
Private Sub changeLabelText(lbl As Label, txt As String)
If lbl.InvokeRequired Then
lbl.Invoke(New changeLabelDel(AddressOf ChangeLabelText), lbl, txt)
Else
lbl.Text = txt
End If
End Sub
Oh, one more thing. You can automate the whole invoke required check for future use. Put the extension method in a module
Method 3: Automate the invoke required pattern, reusable code
<Extension() _
Public Sub InvokeIfRequired(ByVal c As Control, mi As MethodInvoker)
If c.InvokeRequired Then
c.Invoke(mi)
Else
mi()
End If
End Sub
And call it like this:
Public Sub Rename(ByVal labelno As Integer)
Try
Dim lbl As New Label
lbl = Controls("Label" & labelno.ToString())
lbl.InvokeIfRequired(Sub() lbl.Text = ReceivedFrame.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub
There's a wealth of information on this site regarding this topic. See
What does 'InvokeRequired' and 'Invoke' mean in .Net?
VB.net, Invoke, delegates, and threading. Can't figure out how to use them across classes
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…