Optimising InitializeComponent (takes 1 second!)

  • Thread starter Thread starter nzpcmad
  • Start date Start date
N

nzpcmad

There have been a number of posts about techniques to do this. The two
main ones seem to be:

Display from the top down e.g.

//this.Controls.Add(this.buttonEnter);
buttonEnter.Parent = this;

and use fewer commands e.g.

//this.lblStatus.Location = new System.Drawing.Point(111, 218);
//this.lblStatus.Size = new System.Drawing.Size(118, 17);
this.lblStatus.Bounds = new System.Drawing.Rectangle(111, 218, 118,
7);

I have e.g. a basic form that has ten labels and then the value of
each label e.g.

"Ship to" J. Bloggs

and one check box. Nothing else, no tabs etc.

Using Environment.TickCount, I find that it takes around 1 second for
InitializeComponent to complete. This seems a really large number.
Compare this to populating the values which takes around 12
milliseconds.

I tried the techniques mentioned above but they don't seem to have
made any difference. I am using CE 4.1 and CF SP2. I have seen some
posts that suggest that these optimising techniques no longer work
with SP2?

I an trying to optimise my application but this kind of thing causes
real problems.

Any ideas or suggestions?

Thanks
 
Hi,

I have aggresively optimized my application. Here are some tips from my
experiences:

* Forget about the Form designer, instead code your InitComponent by hand.

* Minimize all those slow calls to "new Font()". Just have one (or two if
you need different sizes) global static Tahoma Font around and use that to
set all the properties.

* Set all the Form properties ControlBox, Menu, etc.. before adding any
controls. Setting Form properties often triggers some walking of the control
tree, I guess, and thus is faster if the Form has no controls so far.

* Don't set properties to defaults values they already have (i.e.
aggresively minimize property settings). You would be surprised how few
properties actually have to be set at all. Also remove all those useless
setting of the Name property added by the Form designed.

* I usually set all Event handlers at the very end of InitComponent . So I
won't have useless events fired during setup stage.

Other performance tips:

* If you override OnResize() you should avoid unnecessary layout
recalculations. I usually have a SuspendLayout flag that I have sent during
InitComponent to avoid repeated re-layouts during setup. I also remember the
last width/height that I layouted for an omit the relayout in OnResize if
the width and height did not change. BTW: I always set all control Bounds
dynamically in OnResize only and never in InitComponent.

* Cache your Forms.

* If you override the InputPanel event be aware that you will receive the
event, even if the Form is cached and invisible.

Hope this gives you some ideas.

Cheers,

Andreas
 
Back
Top