4

I have some forms that takes a bit of time to open because they currently get some stuff from a database in their Load event handler.

Is it possible to somehow load the forms in a separate thread and show them to the user when that is done?

If loading them so that the Load event handler is fired is not possible, maybe having a IPreloadable interface might do the trick with a Preload method, and then move the slow loading contents into that. If it is possible to show the form from a separate thread that is... guess I would need to use Invoke or something similar?

Svish
  • 152,914
  • 173
  • 462
  • 620
  • Maybe I'm missing something, but you could Hide() the form first, then fire a BackgroundWorker's StartAsynchronous() and show the form when the BackgroundWorker is done. Seems too simple. :) – KiNgMaR Sep 07 '09 at 18:47

1 Answers1

2

If you load different forms on different threads, you'll have to be very careful when you make calls between the forms - you'll need to use Control.Invoke/BeginInvoke all over the place.

Note that while each top level window can run on a different thread, all the controls within a window must be created (or rather, they must have their handle created) on the thread for that window.

Why not load the database information in the background, and then when that's finished then you can build the actual form and display it? (Until then you might either want to change to a wait cursor, or possibly put a "Loading data..." status message somewhere.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Would of course have to have a status message and maybe a status bar set to marquee or something somewhere while this was happening. But how would you load the database information in the background and then build the actual form? Like, where do you do it? In the constructor instead of the load event handler? Or not in the form at all, but in the parent form or something? – Svish Sep 07 '09 at 18:53
  • Do it using BackgroundWorker, and then have the RunWorkerCompleted event handler use the data that's just been fetched. – Jon Skeet Sep 07 '09 at 19:13
  • So, Show Form with empty controls -> fetch data with BackgroundWorker -> Create missing/populate controls; or fetch data with BackGroundWorker -> Show form and populate controls? – Svish Sep 08 '09 at 15:09
  • Yup. Personally I'd go with the latter, probably. – Jon Skeet Sep 08 '09 at 15:24
  • Fantastic. Will have to try one of those then =) – Svish Sep 16 '09 at 17:29
  • May i ask if there is another way? We use controls from a vendor. If there are a lot of controls the InitializeComponent() while take time. (We do the database attachment later, so its just blank display/init of Components which takes up time). so we would like to do something like Form mainForm; , then a thread mainform = new MainForm(someparamter); show another loginform, and after the login form join back from plain programm to join mainForm and afterwards Application.run(mainForm); – Offler Nov 15 '17 at 08:30