为什么添加SuspendLayout和ResumeLayout会降低性能? [英] Why is adding SuspendLayout and ResumeLayout reducing performance?

查看:173
本文介绍了为什么添加SuspendLayout和ResumeLayout会降低性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在父控件中添加很多控件.

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

但是我发现是否在将这些控件添加到父级之前和之后添加ParentControl.SuspendLayoutParentControl.ResumeLayout,我使用秒表来测量刻度线: 如果删除代码ParentControl.SuspendLayoutParentControl.ResumeLayout,它将更快.为什么会发生?

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?

因此,不应SuspendLayoutResumeLayout减少添加子控件的时间,对吗?那么,使用SuspendLayoutResumeLayout有什么好处,换句话说,如果我不使用SuspendLayoutResumeLayout而是直接将子控件添加到父级,有什么不好?

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?

推荐答案

这是通常的原因,删除代码通常会使您的程序运行更快.

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

Suspend/ResumeLayout()普遍被误解.仅当您的控件具有非默认的AutoSize,Dock或Anchor属性时,它才会起作用.当控件具有相互影响的布局属性时,它可以防止发生布局事故.

Suspend/ResumeLayout() is pretty universally misunderstood. It will only have an affect 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.

这篇关于为什么添加SuspendLayout和ResumeLayout会降低性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆