2. Write another function that decides if it is being called from the UI thread or from another thread-
If its called from its own thread, call the DisplayError() function directly or else invoke it through a delegate
private Sub DisplayErrorOnUI(ByVal errMsg As String)
{
If (Me.InvokeRequired) Then
Dim invokeDelegate As _invokeUIControlDelegate = New _invokeUIControlDelegate(AddressOf DisplayError)
Me.Invoke(_invokeUIControlDelegate, errMsg)
Else
DisplayError(errMsg)
}
3. Declare a delegate in the class (or form in this case) to the above function (i.e DisplayErrorOnUI())
Private Delegate Sub _invokeUIControlDelegate(ByVal errMsg As String)
4. In the background thread call the InvokeUIControl() function as required
Try
// do something here that gives error
Catch ex As Exception
DisplayErrorOnUI(ex.Message)
End Try