. . Hong Kong Business Software Company
Expert in web-based solutions
Software House in Hong Kong

Microsoft Certified Solution Developer

 

Chief Architect's Blog on software development

10 March 2008
Writing message from non-UI thread to UI controls
If you write a message to a form control from a calling thread which is not the same as the UI thread, invoke() method of the form or control must be used to interact with its methods and properties. Receiving messages from com port and callback from WCF or Web Service are examples of services not running on UI thread. The InvokeRequired property of forms and controls can be used to check whether the calling thread is not the same as the thread where the form or control was constructed. The following is an example of updating UI by a WCF callback.
'Procedure awaiting callback from host
Public Sub Callback(ByVal message As String) _
    Implements IServiceCallback.Callback
       
       If Me.InvokeRequired Then
           Me.BeginInvoke(New ObjectToUI(AddressOf UpdateUI), message)
       Else
           Label1.Text = message
       End If

End Sub
 
Public Delegate Sub ObjectToUI(ByVal o As Object)
 
Private Sub UpdateUI(ByVal m_message As Object)
        Label1.Text = CType(m_message, String)
End Sub