Re: Splash Screen - My Nemesis
Fabian Schmied <[email protected]> Thu, 26 Jul 2007 18:43:11 +0200
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
> I think now, to achieve the clients requirement of immediate feedback, I > will forego the splash screen and show the main form immediately, delegating > the database interaction to a separate thread started by the main form. > > Now, do I do this in the Load event, as I need the form to already be > visible before I spawn another thread? I think this is actually a very good approach - you can show the main form (disable the controls while no interaction is allowed), indicating your application is up and running, but provide some kind of visual hint that the application is busy talking to the database. About the thread spawning: don't do this yourself, use a BackgroundWorker instead. This is a component you can put on your form. Subscribe to its DoWork event and put your database code in there, this will be executed on a background thread. Do not access any controls from that event, just get the results of the database query and put it in the DoWorkEventArgs.Results member. If you get an error, put it in the Results member instead. Subscribe to the BackgroundWorker's RunWorkerCompleted event, it will be fired when DoWork has finished. Its event args will hold the Result you previously assigned in DoWork. You can update your UI from this event and either show an error message or the data retrieved from the database. Start the BackgroundWorker using RunWorkerAsync. If you need, you can pass an argument to your DoWork method. There should be no problem calling RunWorkerAsync from the form's OnLoad method. Shouldn't be too hard that way, and it's easy to add progress indicated or cancelation later, if needed (and possible). Regards, Fabian