Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Windows Forms Components (Performance problem)

I have a problem with performance of my code under Windows Forms. Have a form, her layout is depending on constructor data, because he layout must be OnLoad or in Constructor generated.

I generation is simple, base FlowLayoutPanel have other FlowLayoutPanels, for each have a Label and TextBox with DataBinding.

Problem is this is VERY SLOW, up to 20 seconds, i drawing less than 100 controls, from Performace Session i know a problem is on 70% procesing functions:

  1. System.Windows.Forms.Control.ControlCollection.Add(class System.Windows.Forms.Control)
  2. System.Windows.Forms.ControlBindingsCollection.Add(class System.Windows.Forms.Binding)

How i can do with this? Anyone help me in this problem? How solve the dynamic form layout problem?

like image 268
Svisstack Avatar asked Dec 22 '25 07:12

Svisstack


2 Answers

Have you disabled layout while adding the controls?

        panel.SuspendLayout();
        try
        {
            // add controls in a loop/etc here
        }
        finally
        {
            panel.ResumeLayout();
        }
like image 164
Marc Gravell Avatar answered Dec 23 '25 21:12

Marc Gravell


I recently read the this article that has a ton of ideas for optimizing performance in .Net Winforms apps. MSDN Article

Some of those that may apply to your situation are that if you are retrieving the data that you are Databinding the Controls to you may want to spin that off onto a sep. thred from the UI so it can retrieve the Data Asynchronously.

You can do this with the Background Worker class.

Also if only some of the controls are not going to be initially visible you can postpone that work until later.

"Operations that must be performed, but are not needed for displaying the first "UI, can be done while the system is idle or on demand after the first UI is shown. "For example, if you have a TabControl, populate only the topmost page on startup "and retrieve the information for other pages when required."

like image 41
etoisarobot Avatar answered Dec 23 '25 19:12

etoisarobot