I need to do this all the time and don’t have the best memory in the world. Today, I decided that I looked this up one too many times so here’s my solution to this multithreading problem:
The Problem:
You try to modify UI components created in one thread in another thread. In VB, you can only make UI changes on the same thread that created it so you get a nice “Cross thread operation not valid” exception.
Here’s the wrong code:
thread = New System.Threading.Thread(AddressOf DoStuff)
thread.Start()Private Sub DoStuff() 'error occurs here' Me.Text = "Stuff" End Sub