You can use SynchronizationContext to do this. Store the UI tread context in a variable like this
Private syncContext As SynchronizationContext Private Sub frmClient_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown syncContext = AsyncOperationManager.SynchronizationContext End Sub
Now create a procedure to execute in the main user interface thread, for example
Private Sub AddTextBox() 'Do whatever you want you are in UI thread here End Sub
Your request to send a background thread to a UI thread like this
syncContext.Post(New SendOrPostCallback(AddressOf AddTextBox), Nothing)
you can even pass arguments as well
Private Sub AddTextBox(ByVal argument As Object) 'Do whatever you want you are in UI thread here End Sub ..... syncContext.Post(New SendOrPostCallback(AddressOf AddTextBox), objToPass)
source share