I have an application that needs to wait for a background worker to finish before moving on. The background worker is in charge of writing some values to a USB device that initializes it. The problem with the background worker is that my main program can try to access the USB device before the background worker is finished initializing.
While the background worker is working, the UI thread shows a “please wait” with an animated progress bar. The progress bar does not reflect how far along the background worker is, it merely ‘spins’.
I have read a few questions that say not to use a background worker, because I really don’t want it to run asynchronously (which is true), however, without using a background worker, my “please wait” dialog blocks and doesn’t show the animation. I have also read a lot that tells me to only use one UI thread, which then supports the use of a background worker.
I have also tried to put the “please wait” spinner in a separate thread, but that introduces complexities and strange race conditions where the “please wait” windows tries to close before it has opened.
What is the right way to go about this?
That’s exactly what
BackgroundWorkeris designed for. Although there are other ways to accomplish this, what you’re doing is just fine.