Crossthread operation not valid… – VB.NET

The purpose of the BackgroundWorker class is to perform work on a non-GUI thread while the GUI remains responsive. Unless you set Control.CheckForIllegalCrossThreadCalls to false (which you shouldn’t do), or use Invoke as suggested in the other answers (which I also wouldn’t recommend), you’re going to get an illegal cross-thread operation exception.

If you want GUI-related “stuff” to happen while your BackgroundWorker is running, I’d generally recommend using the BackgroundWorker.ReportProgress method and attaching an appropriate handler to the BackgroundWorker.ProgressChanged event. If you want something on the GUI to happen once the BackgroundWorker is finished, then simply attach your handler to update the GUI to the BackgroundWorker.RunWorkerCompleted event.

Leave a Comment