views:

199

answers:

2

Hi,

Would it be theoretically possible to make BackgroundWorker in a class to periodically refresh existing splash screen form, or is that impossible? (I know that it's probably bad design, but currently I do not see any better way.)

Please keep in mind that:

  1. I do not want the background worker to do the loading as it would be terribly hard to implement.

  2. I can't use the inbuilt splash screen support

  3. I'm aware of DoEvents alternative but I do not want to go this path, it would very hard to implement as well, and not reliable.

+2  A: 

It is absolutely possible, and if it's not quite what BackgroundWorker was intended for (primarily, refreshing a foreground GUI while a long-running background process is taking place), it's pretty darned close.

If you're trying to do something like make a splash screen rotate through images or text while your application or loading or installing, consider just putting a timer on the splash, and refreshing on timer tick.

You're wise to stay away from DoEvents, which seems the simplest solution at first, but leads to pain and intermittent, hard-to-debug problems down the road.

Edit: From your comments, it looks like you're loading your application on the splash screen's GUI thread, and that's causing your splash form to not refresh itself. This is expected behavior. You'll want to put your application load onto a background thread, using bare threading or BackgroundWorker (which is designed for exactly this situation). I'll bet it's not as difficult to do as you're expecting it to be. If you're having problems with that approach, feel free to post a question asking for help.

Michael Petrotta
Thanks for your insight. I just need to refresh the splashscreen periodically so it redraws, as it turns white when operation that takes long is being executed. Unfortunately, timer doesn't quite work.
Jiri
@Jiri: Ah. That changes things - I see what you're doing now. It won't work. Remember that background worker for option 1, that seemed hard to implement? You'll have to do that. It's the right way to do things, and I'll bet it's not all that hard - I've done it many times. If you're having trouble with that approach, feel free to describe why.
Michael Petrotta
+1  A: 

If your SplashScreen is static it doesn't need anything (like a bgw) to hold it up. Just create it, Refresh() it and start preparing your mainForm.

But yes, you could run a secondary messageloop in another Thread (I'm not sure if a BackgroundWorker is possible or desirable). Basically, start a Thread with:

void ThreadedProc()
{ 
    var f = new SplashForm();
    Application.Run(f);  // will terminate when 'f' closes
}
Henk Holterman
Thanks for your answer. It is static, but I need to refresh it periodically so it redraws itself properly when long operations are being executed. Accessing the form from _DoWork of a bgw seems not so easy though.
Jiri