4

How do I speed optimize Windows Forms applications?

I am not talking about apparent .NET optimisation techniques - like NGEN-ing, caching objects, etc. I already tried that and what I am up to is to reduce the form initilization time from a 1500 msec down to 500 msec.

Profiling has identified the slowest code and almost all of it is in the InitializeComponent, and within this method the slowest lines is

  1. creation of the (just new-ing) WebBrowser component
  2. loading icon from a resource (hideous 500 msec)
  3. creation of the ContextStripMenu
  4. several this.Controls.Add() calls contribute a lot too.

At the moment, I can only see how to fix point (2) - move icon data from being stored as embedded resource to a private field (for example, Base64-encoded string).

What what should I do with points 1, 3 and 4?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Is the 1500ms only the first time the forms is instantiated or every time? – Rauhotz Feb 15 '09 at 23:10
  • Not sure, but the idea is to eliminate delays the first time form is show. There might be no second chance if app is slow. –  Feb 16 '09 at 00:40
  • that is what splash-screens are for - to distract the user while the app loads for the first time ;-) – Steven A. Lowe Mar 17 '09 at 05:54

6 Answers6

1

The only thing I can think of that you could do is rewrite the controls that you want to use and optimize them to initialize faster (as well as the Form class to optimize adding the adding of the controls to the form).

I can't see how that is feasible though, and I think you are going to be stuck with this, depending on your reliance on these controls.

casperOne
  • 73,706
  • 19
  • 184
  • 253
1

Load the icon in a separate InitializeComponentAsync thread.

yfeldblum
  • 65,165
  • 12
  • 129
  • 169
0

Can you do lazy loading for your Webbrowser control? If it's in a tab which is not the main view then you might load webbrowser when that tab is activated.

Or you might load the form and then load the webbrowser (this might help you to show something first and then show everything, just like you'd do in a website).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dr. evil
  • 26,944
  • 33
  • 131
  • 201
0
  1. Just take another class like ClsAppearance.cs as I taken.

  2. Initialize all controls like

    static Infragistics.Win.Appearance txtBoxMidAppr = null;
    

    I take my own name like txtBoxMidAppr instead if the appiarance1. due to it can be use for all textbox, by only once initialization.

  3. Make a function where we can initialize the appearance and call it on the MDI/Main form loading only once.

    public static void LoadAll()
    {
        txtBoxMidAppr = new Infragistics.Win.Appearance();
    }
    
  4. Make another function here and take the appearance code from designing window

    public static Infragistics.Win.Appearance App_txtBoxMidAppr //text_box_small
    {
        get 
        {
            txtBoxMidAppr.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(93)))), ((int)(((byte)(93)))), ((int)(((byte)(93)))));
            txtBoxMidAppr.ImageBackground = global::CS_POS.Properties.Resources.text_box_small;
            txtBoxMidAppr.ImageBackgroundStyle = Infragistics.Win.ImageBackgroundStyle.Stretched;
            txtBoxMidAppr.ImageHAlign = Infragistics.Win.HAlign.Right;
            return txtBoxMidAppr;
        }
    }
    
  5. In the designing code of the form, comment all appearance setting of the text box and put the function name for getting appearance from the ClsAppearance.cs class

    //appearance4.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(93)))), ((int)(((byte)(93)))), ((int)(((byte)(93)))));
    //appearance4.ImageBackground = global::CS_POS.Properties.Resources.text_box_small;
    //appearance4.ImageBackgroundStyle = Infragistics.Win.ImageBackgroundStyle.Stretched;
    //appearance4.ImageHAlign = Infragistics.Win.HAlign.Right;
    this.uteNABoth.Appearance = CS_POS.App_Appearance.ClsAppearance.App_txtBoxMidAppr;
    

    take all controls appearance and make a function in the class and call it from there.

So the appearance initialization will goes only once and can use many time.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
0

I have changed the strategy of form loading, this will make a great change on form load timing, now it's taking an average of 37 ms instead of 466 ms.

Method: On First time Click on Top-tab/Icon, application load all form under that tab/icon and on click on Form Icon it will only switch visibility. And again visit on Top-tab will not load the form under that tab.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
0

One technique I have used in the past was to multi-thread the data load, so that it runs simultaneously to the form creation. In this instance data was being loaded out of AD, it cut about 1/3 of the load time.

benPearce
  • 37,735
  • 14
  • 62
  • 96