You cannot access any GUI components from the BackgroudWorker. For example if you try to do something like this
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
TextBox1.Text = "Done"
End Sub
you will receive a runtime error saying that "Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on."
This is because the BackgroundWorker runs your code on another thread in parallel with the main thread, and the GUI components are not thread-safe. You have to set your code to be run on the main thread using the Invoke
method, giving it a delegate:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
Me.Invoke(New MethodInvoker(Sub() Me.TextBox1.Text = "Done"))
End Sub
Or you can use the ReportProgress method of the BackgroundWorker:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
Me.BackgroundWorker1.ReportProgress(0, "Done")
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
Me.TextBox1.Text = DirectCast(e.UserState, String)
End Sub