Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is the better place to do initialization work in winform?

In the Load event or in the constructor after the InitializeComponent()?

or doesn't matter at all?

like image 396
Benny Avatar asked Jan 15 '10 03:01

Benny


5 Answers

The Form.Shown event is a good place to do any initialization that may take more than about one second. Form.Shown occurs once only, just after the form is first made visible to the user.

Obviously if you have a lengthy initialization you still need to provide some sort of visual feedback and perhaps disable sections of the form until complete. But if the initialization is unavoidable, Form.Shown at least allows you to let the user know the application has not frozen and give feedback on what it is actually doing.

Compared to Form.Load: From the users perspective your application will be perceived to start up more quickly because your Form is already visible while initialization is done.

Compared to Form.Activated: You don't need to worry about your initialization running multiple times because the Activated event is called every time your form is hidden/shown, minimized/maximized etc.

Compared to the constructor: Similar to Form.Load your form will not be visible until initialization is complete. Also, you must be more careful about timing/sequence issues related to controls that may not be fully initialized.

like image 115
Ash Avatar answered Oct 18 '22 22:10

Ash


Heavier initialization may also not be a good idea in load event as it may increase load time and can be irritating to end user. I prefer doing a basic initialization (say..which takes <10 seconds) in Form Load and do rest of the heavy work after it is displayed to the user. To make user wait a progress indicator can be displayed.

like image 39
geekonweb Avatar answered Oct 18 '22 21:10

geekonweb


For heavier initialization it's typically done in the load event. Constructors are usually used for quick, simple initialization of fields. If you have to make a method call to an external dependency for example, you should do it from load.

like image 45
Paul Sasik Avatar answered Oct 18 '22 20:10

Paul Sasik


It depends on the kind of initialisation. Simple field initialisation, for example, can be done in the constructor and this saves you from having to hook up an event, have an additional method, etc.

However, in some cases the constructor doesn't have the info you need. For example, if you want to do different things depending on whether you're in design mode or run mode (e.g. in run mode you will connect to a data source, but in design mode you want to display sample data), then this must be deferred until after construction, because the framework doesn't set DesignMode until after the object has been constructed.

like image 2
itowlson Avatar answered Oct 18 '22 20:10

itowlson


Doing stuff in the Load event will make the exceptions when it fails slightly easier to read and IMHO is semantically cleaner. Practically, it doesn't make a ton of difference.

like image 1
Ana Betts Avatar answered Oct 18 '22 21:10

Ana Betts