如何改善winform的加载时间? [英] How to improve the loading time of winform?

查看:33
本文介绍了如何改善winform的加载时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WinForms 应用程序.主窗体有很多控件,这也是它加载非常缓慢的原因之一.我想做的是使表单加载速度更快.

I have a WinForms application. the main form is has a lot of controls and that is one of the reasons that makes it load very slow. what I would like to do is to make the form load faster.

我已经设置了 beginupdate 和 endupdate.该表单未在后台工作线程中呈现,因为这是主表单.没有初始形式.当用户单击应用程序图标时,这是加载的第一个表单.添加进度条或任何启动表单对我来说都不是一个好主意.

I have set the beginupdate and endupdate. The form is not being rendered in the background worker thread, because this is the main form. There are no initial forms. When the user clicks the application icon, this is the first form that loads up. Adding a progress bar or any splash form is not a good idea for me.

我在这里检查了有关堆栈溢出的其他问题,但他们似乎没有遇到与我相同的问题.

I have checked other questions here on Stack overflow but they do not seem to face the same problem as I do.

如果你有一些例子/想法,如果你能与我分享,那将是很好的.

If there are some examples/ideas you have in mind, it would be nice of you if you can share it with me.

推荐答案

几点建议:

  • 尽量减少 UI 的复杂性.您的用户会感谢您,您可以加载更少的控件.例如,如果您有 3 或 4 个不经常使用的控件,您能否将它们移动到对话框或折叠表单的高级"部分,以便推迟创建/显示它们?是否需要所有控件?真的吗?想一想您要实现的工作流 - 当前的一组控件是实现工作流的最简单方法吗?是否需要同时显示所有控件?也许您可以将它们放在选项卡控件中的单独选项卡上(因此实际上只能在显示选项卡时创建控件)?

  • Try to minimise the complexity of your UI. Your users will thank you and you'll have fewer controls to load. For example, if you have 3 or 4 controls that are not used often, can you move them into a dialog or fold-out "advanced" section of your form, so you can defer creating/showing them? Are all the controls needed? Really? Think about the workflow you are trying to achieve - is the current set of controls the simplest way to achieve the workflow? DO all the controls need to be shown at once? Perhaps you could place them on to separate tabs in a tab control (and thus only actuallyl create the controls as the tab is shown)?

你能减少使用的控件类型的范围吗?每一种新的控件类型都可能导致您的程序加载一个新的 dll 来支持它.每个必须初始化的 dll 都会导致额外的启动时间.

Can you reduce the range of control types used? Each new type of control may cause your program to load up a new dll to support it. Every dll that has to be initialised causes extra startup time.

您是否使用任何启动缓慢的控件?简单的文本字段会很快,但复杂的图形控件可能会很慢.

Are you using any controls that are slow to start up? A simple text field will be fast, but a complex graphing control may be slow.

加载了多少个程序集(您自己的)?将所有代码组合成一个程序集(例如使用 ILMerge),加载时间可能会得到很大改善.

How many assemblies (of your own) are loaded? Combine all the code into a single assembly (e.g. with ILMerge) and load times will probably improve quite a bit.

删除所有不需要的初始化代码.你能简化初始化吗?是否可以推迟任何初始化(例如,仅在用户单击实际需要存在该数据的第一个按钮时才创建一些成员变量,如果实际上还不需要,请不要尝试创建与数据库的连接等)

Remove any initialisation code that isn't needed. Can you simplify the initialisation? Can any initialisation be deferred (e.g. only create some member variables when the user clicks on the first button that actually needs that data to be present, Don't try to create a connection to a database if it's not actually needed yet, etc)

你能推迟(部分)UI 的创建吗?例如,您可以将一组控件放入一个单独的 UserControl 表单中,然后在启动后不久(例如在计时器上)以编程方式将此表单添加到您的 MainForm 中.这将使您的 MainForm 快速出现,然后在不久之后填充"其他控件,这可能不会改善实际启动时间,但它会感觉"更快,启动时响应更快.(如果您的 MainForm 滚动并且那些额外的控件最初不在屏幕上,这种方法也非常有效,因为只有在用户向下滚动到足以看到它们时才需要创建它们)

Can you defer creation of (some of) the UI? For example, you may be able to place a group of controls into a separate UserControl form, and then add this form programmatically to your MainForm shortly after startup (e.g. on a Timer). This will allow your MainForm to appear very quickly, and then be "populated" shortly after with additional controls, which may not improve the actual startup time, but it will "feel" a lot faster and more responsive to start up. (This approach can also be extremely effective if your MainForm scrolls and those extra controls are initially not on-screen, as they only need to be created if the user scrolls down enough to see them)

您是否显示任何可能加载缓慢的信息(例如大型位图图像或从 SQL 服务器获取的数据)?您可以推迟加载它们或将其作为后台线程运行吗?使用压缩来加速加载?降低它们的分辨率以最小化必须加载的数据量?对数据进行预处理并将其存储在快速启动缓存中以备下次程序运行时使用?

Are you displaying any information that might be slow to load (e.g. large bitmap images or data fetched from an SQL server)? Can you defer the loading of them or run it as a background thread? Use compression to speed up loading? Reduce their resolution to minimise the amount of data that must be loaded? Pre-process data and store it in a quick-start cache for the next time the program is run?

某些控件可以用优化的方法代替吗?例如您可以将按钮栏"创建为一组 10 个单独的控件,或者创建为具有 10 个按钮外观的单个控件.使单个控件的初始化和重绘速度比 10 个单独的控件要容易得多.

Can some controls be replaced by an optimised approach? e.g. You can create a "button bar" as a set of 10 separate controls, or as a single control that draws iself with the appearance of 10 buttons. It's much easier to make the single control initialise and redraw faster than 10 separate controls.

当然,一旦收集到最明显的低垂果实(甚至更早):

And of course, once the most obvious low-hanging fruit has been collected (or even before):

  • 在分析器下运行程序,看看它把时间花在哪里了.

这篇关于如何改善winform的加载时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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