views:

219

answers:

2

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?

+2  A: 

Have you disabled layout while adding the controls?

        panel.SuspendLayout();
        try
        {
            // add controls in a loop/etc here
        }
        finally
        {
            panel.ResumeLayout();
        }
Marc Gravell
Yes i have it, on base panel but application works in same time, if I get this into a form result is same.
Svisstack
+1  A: 

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

TooFat
Yes, this is a good idea, but actually on my form all content is visible, but this is good tip for future.
Svisstack