-2
Public Sub Rename(ByVal labelno As Integer)
    Try
        Dim lbl As New Label
        lbl = Controls("Label" & labelno.ToString())
        lbl.Text = ReceivedFrame.ToString()
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

i want to change Label Text from 1 to 50. Label no given as input. i tried the above code. its working in other project. but in my current project it is not working Error is showing like this (cross Thread operation not valid.control label1 accessed from a thread.other than the thread it was created on)and i want to change the label text only.label&Label no is working properly i verified. is there any other way to rename label in vb.net?

1 Answers1

0

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

Community
  • 1
  • 1
djv
  • 15,168
  • 7
  • 48
  • 72