We have the scenario where multiple labels values are being set from a different thread. When this happens an exception is thrown as labels (and most other controls) are not thread safe. To get around this you can have the hosting form do a BeginInvoke to queue the message to the proper thread (I think that internally it does a SendMessage to the form to do the actual work).
To do this I create a delegate to a method that sets the labels value:
Private Delegate Sub SetLabelDelegate(ByVal value As String, ByVal label As Label)
Then I create the method:
Private Sub SetLabel(ByVal value As String, ByVal label As Label)
label.Text = value
End Sub
Now just to make things simple I always just call SetLabel() but inside that method I check if we require an invocation (i.e. we are calling the method from a different thread). Final code looks like this:
Private Delegate Sub SetLabelDelegate(ByVal value As String, ByVal label As Label)
Private Sub SetLabel(ByVal value As String, ByVal label As Label)
If Me.InvokeRequired() Then
Me.BeginInvoke(New SetLabelDelegate(AddressOf SetLabel), New Object() {value, label})
Else
label.Text = value
End If
End Sub