When you call _fWait.Close()
is has to be called on the UI thread whereas the BackgroundWorker1_DoWork
handler will be running on a different thread which is why you are getting the error. There are two options for closing the form:
1) Continue to close the form on the DoWork
handler but marshal the request onto the UI thread using something like the code below:
this.Invoke(() => _fWait.Close());
It's been a while since I've done VB, so you'll have to convert the C# code into VB...sorry.
2) Handle the RunWorkerCompleted
event and close it there. If you BackgroundWorker was started on
the UI thread, then the RunWorkerCompleted
will also be called on the UI thread.
If I remember right the ProgressedChanged
event will also be called on the UI assuming that the BackgroundWorker
was created on the UI thread. If the BackgroundWorker
is created another another thread, then the RunWorkerCompleted
and ProgressChanged
will also be called on a separate thread and you will have to marshal calls to the UI as described above in step 1.