Optimizer
Optimizer

Reputation:

Speed-optimise a Windows Forms application

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?

Upvotes: 4

Views: 8125

Answers (6)

dr. evil
dr. evil

Reputation: 27285

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).

Upvotes: 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.

Upvotes: 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.

Upvotes: 0

casperOne
casperOne

Reputation: 74560

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.

Upvotes: 1

benPearce
benPearce

Reputation: 38373

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.

Upvotes: 0

yfeldblum
yfeldblum

Reputation: 65455

Load the icon in a separate InitializeComponentAsync thread.

Upvotes: 1

Related Questions