10

I have a WinForms application. the main form is has a lot of controls and that is one of the reasons that makes it load very slow. what I would like to do is to make the form load faster.

I have set the beginupdate and endupdate. The form is not being rendered in the background worker thread, because this is the main form. There are no initial forms. When the user clicks the application icon, this is the first form that loads up. Adding a progress bar or any splash form is not a good idea for me.

I have checked other questions here on Stack overflow but they do not seem to face the same problem as I do.

If there are some examples/ideas you have in mind, it would be nice of you if you can share it with me.

TK-421
  • 294
  • 1
  • 7
  • 26
Gagan
  • 5,416
  • 13
  • 58
  • 86
  • 2
    I find it hard to believe that loading regular controls is taken a long time. It isn't some other things that's loading slowly? Show some code – Oskar Kjellin Jul 30 '11 at 18:12
  • 1
    It isn't the loading that's slow, the painting takes forever with a lot of controls. Use less controls. – Hans Passant Jul 30 '11 at 18:43

5 Answers5

13

A few suggestions:

  • Try to minimise the complexity of your UI. Your users will thank you and you'll have fewer controls to load. For example, if you have 3 or 4 controls that are not used often, can you move them into a dialog or fold-out "advanced" section of your form, so you can defer creating/showing them? Are all the controls needed? Really? Think about the workflow you are trying to achieve - is the current set of controls the simplest way to achieve the workflow? DO all the controls need to be shown at once? Perhaps you could place them on to separate tabs in a tab control (and thus only actuallyl create the controls as the tab is shown)?

  • Can you reduce the range of control types used? Each new type of control may cause your program to load up a new dll to support it. Every dll that has to be initialised causes extra startup time.

  • Are you using any controls that are slow to start up? A simple text field will be fast, but a complex graphing control may be slow.

  • How many assemblies (of your own) are loaded? Combine all the code into a single assembly (e.g. with ILMerge) and load times will probably improve quite a bit.

  • Remove any initialisation code that isn't needed. Can you simplify the initialisation? Can any initialisation be deferred (e.g. only create some member variables when the user clicks on the first button that actually needs that data to be present, Don't try to create a connection to a database if it's not actually needed yet, etc)

  • Can you defer creation of (some of) the UI? For example, you may be able to place a group of controls into a separate UserControl form, and then add this form programmatically to your MainForm shortly after startup (e.g. on a Timer). This will allow your MainForm to appear very quickly, and then be "populated" shortly after with additional controls, which may not improve the actual startup time, but it will "feel" a lot faster and more responsive to start up. (This approach can also be extremely effective if your MainForm scrolls and those extra controls are initially not on-screen, as they only need to be created if the user scrolls down enough to see them)

  • Are you displaying any information that might be slow to load (e.g. large bitmap images or data fetched from an SQL server)? Can you defer the loading of them or run it as a background thread? Use compression to speed up loading? Reduce their resolution to minimise the amount of data that must be loaded? Pre-process data and store it in a quick-start cache for the next time the program is run?

  • Can some controls be replaced by an optimised approach? e.g. You can create a "button bar" as a set of 10 separate controls, or as a single control that draws iself with the appearance of 10 buttons. It's much easier to make the single control initialise and redraw faster than 10 separate controls.

And of course, once the most obvious low-hanging fruit has been collected (or even before):

  • Run the program under a profiler and see where it's spending its time.
Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • 3
    I ran the profiler and the almost 52% of the delay was due to the form Initialization. I have an IDE like environment where there are tons of other controls that the user might need at any given time.. that is the problem.. – Gagan Jul 30 '11 at 22:31
  • 1
    However I do like your ideas.. will try to implement most of them .. however ..I have decided to use a background worker thread to help load some of the controls – Gagan Jul 30 '11 at 22:36
4

Try to minimize the code that executes during on load of main form or any of the control that is placed on the main form.

You can also explore NGEN which is Microsoft's tool which helps in improving managed app's performance

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • 1
    I did use NGEN. Though the time period to load the UI has decresed still i would like the UI to be displayed as soon as the user launches the application – Gagan Jul 30 '11 at 22:32
  • 3
    i checked with and without ngen.. the start up without NGEN was 7225 milliseconds.. after running this ngen command ngen install the startup time decreased by more than half. now its 3015.. milliseconds – Gagan Jul 30 '11 at 22:49
0

When a form loads it initializes all its controls.
The Form itself isn't taking you a long time.. It's your controls.

Go over your controls and check what can be improved in their constructors and initializers.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • 1
    true, its the controls .. i am using a docking library, some Krypton Controls. they are feature rich but they also come with a memory tag. have seen some other applications using the same set of controls but they load pretty fast. I was wondering if I could do the same... – Gagan Jul 30 '11 at 22:34
0

Do you need all of the controls immediately? If not perhaps you could load them programmatically after some event fires that lets you know you need that control.

Paul
  • 948
  • 8
  • 17
0
  • If you have several controls to a parent control, call the SuspendLayout method before initializing the controls to be added.
  • After adding the controls to the parent control, call the ResumeLayout method. This will increase the performance of applications with many controls.

For example:

private void LoadData()
{
   // Suspend the form layout and load the data
   this.SuspendLayout();
   LoadMyData();   // logic to load your data will be here
   this.ResumeLayout();
}

EXPLANATION:

  1. SuspendLayout() - Stops the layout object from being updated and thus the component does not spend any time making calculations for repainting until the layout is resumed.
  2. ResumeLayout() - Recomputes the layout once after all of your changes are made, resulting improvement in performance.

Why use SuspendLayout() and ResumeLayout()

  1. It prevents layout accidents when controls have layout properties that affect each other.
  2. It adjusts multiple layout-related properties like Dock, Auto-Size etc.
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Rohan Rao
  • 2,505
  • 3
  • 19
  • 39