Windows Forms Dynamic Components (performance issue)

I have a performance problem with my code under Windows Forms. You have a form, its layout depends on the data of the constructor, because it must be OnLoad or generated in the Constructor .

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

The problem is that it is VERY SLOW, up to 20 seconds, I draw less than 100 controls, from the Performace Session I know that the problem is related to 70% of the processing functions:

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

How can i do this? Anyone help me on this issue? How to solve the problem of placing a dynamic form?

+4
source share
2 answers

I recently read this article, which has a ton of ideas for optimizing performance in .Net Winforms applications. MSDN Article

Some of the ones that may apply to your situation are that if you are retrieving data that you attach to controls, you may need to unscrew it into a separate segment. from the user, so he can retrieve data asynchronously.

You can do this using the Background Worker class.

Also, if some of the controls are not initially visible, you can defer this work to the end.

"Operations that must be performed but not needed to display the first" User Interface "can be performed when the system is in standby mode or on demand after the first user interface is displayed." For example, if you have a TabControl, fill out only the top page at startup, "and retrieve information for other pages when necessary."

+2
source

Do you have the layout disabled when adding controls?

  panel.SuspendLayout(); try { // add controls in a loop/etc here } finally { panel.ResumeLayout(); } 
+3
source

Source: https://habr.com/ru/post/1308038/


All Articles