Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve the loading time of winform?

Tags:

c#

winforms

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.

like image 736
Gagan Avatar asked Jul 30 '11 18:07

Gagan


People also ask

Why is my WinForms form so slow?

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.

How to improve the startup performance of a form?

The perception of startup performance depends largely on the delay until the first UI is shown. You should minimize the logic required to display the UI. This includes any work performed in the Load event of the form because this is evaluated as the form is in the process of showing itself.

How can I optimize performance for my application?

It may seem cheeky to list this as the first item, but it is meant quite seriously: among the most effective things you can do to optimize performance for your application is to use our latest release. We constantly allocate significant developer time to an ongoing process of performance analysis and improvement.

How long does it take to load rmeb forms?

In the case of the RMEB the time to just load the form is the same whether or not the RMEB's are populated. The issue I have is that the forms will be opened and closed a fair bit during use and so the initial form load needs to be quick.


1 Answers

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.
like image 71
Jason Williams Avatar answered Sep 23 '22 21:09

Jason Williams