Why is adding SuspendLayout and ResumeLayout reducing performance?
Asked Answered
B

2

13

I need to add a lot of controls to a parent control.

But I find if I add ParentControl.SuspendLayout and ParentControl.ResumeLayout before and after I add those controls to the parent, I use stopwatch to measure the ticks: If I remove the code ParentControl.SuspendLayout and ParentControl.ResumeLayout, it will be faster. Why does it happen?

So SuspendLayout and ResumeLayout are not supposed to reduce the time to add sub controls, right? So what's the benefit to use SuspendLayout and ResumeLayout or in other words, if I don't use SuspendLayout and ResumeLayout but add the sub controls directly to parents, what's the bad?

Bacillus answered 29/10, 2012 at 13:54 Comment(6)
How many controls are you adding? If it's just a few then there's no need, but if you're adding hundreds/thousands you're more likely to see a difference. Also, if you timed it what was the difference between the two times; was it just some tiny amount (enough to be just random variance in execution times) or was it really significant?Flip
Which types of controls are you using? Similar problem is described here.Part
yes, I add hundreds controls.Bacillus
I think you need to show both versions of the code for any attempt at a real answerVerruca
hard to determine without a code. I use the same method, adding hundreds of dynamic controls very fast without issues while SuspendLayoutMathematician
We call suspend and resume layout command to avoid firing events while adding any controls. Moreover, sometimes they can cause flicker while adding controls if u don't suspend the controlHistoricism
T
10

This is for the usual reason, removing code usually makes your program run faster.

Suspend/ResumeLayout() is pretty universally misunderstood. It will only have an effect when you have controls that have a non-default AutoSize, Dock or Anchor property. It prevents layout accidents when controls have layout properties that affect each other.

If you have a form with hundreds of controls then it is very unlikely that you use these properties at all. Such a massive window does not easily lend itself to automatic layout. So you are calling methods that don't actually do anything, they take time to iterate the layout but for no benefit.

Tennies answered 7/12, 2012 at 1:37 Comment(0)
G
19

You probably want to use .ResumeLayout(false) instead. Calling mySubPanel.ResumeLayout() equals to .ResumeLayout(true), which means it should re-layout this control (and everything child-control that is not suspended at that point) immediately.

MSDN quote: "Calling the ResumeLayout method [without parameters] forces an immediate layout if there are any pending layout requests." [1]

If you are like adding 100 controls to a panel, you want to use an approach like this:

  1. mainPanel.SuspendLayout()
  2. create child control
  3. call child.SuspendLayout()
  4. change the child control properties
  5. add the child control to the mainPanel
  6. call child.ResumeLayout(false) - this means: next layout run, relayout this control, but not immediately
  7. repeat (2-6) for every child-control
  8. call mainPanel.ResumeLayout(true) - this means: relayout my mainPanel and every child-control now!

Note: without SuspendLayout(), every property change for a control would invoke the layout-routine -- even changing the .BackColor makes your control re-layout itself.

[1] http://msdn.microsoft.com/en-us/library/y53zat12.aspx

Genny answered 23/8, 2013 at 11:45 Comment(1)
This helps especially incase if we are iterating through(loop) lot of controls to do property change. Great effect on the loop case. Thanks!Cheekbone
T
10

This is for the usual reason, removing code usually makes your program run faster.

Suspend/ResumeLayout() is pretty universally misunderstood. It will only have an effect when you have controls that have a non-default AutoSize, Dock or Anchor property. It prevents layout accidents when controls have layout properties that affect each other.

If you have a form with hundreds of controls then it is very unlikely that you use these properties at all. Such a massive window does not easily lend itself to automatic layout. So you are calling methods that don't actually do anything, they take time to iterate the layout but for no benefit.

Tennies answered 7/12, 2012 at 1:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.