如何让我的C#windows应用程序轻量级? [英] How to make my C# windows application lightweight?

查看:74
本文介绍了如何让我的C#windows应用程序轻量级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计我已经在C#windows窗体应用程序中开发了一个票务管理系统。现在这是我第一次开发出可能在现实世界中使用的任何应用程序(我的学校可能会实现它)。



因为我的学校并不富裕主要由慈善机构运营,所以他们使用的技术已经很老了(Pentium 4 pcs,Windows XP最大内存为1 GB),所以我需要尽可能轻量化我的应用程序,即它不应该是资源匮乏。 (我已经在具有4 GB RAM的双核笔记本电脑上开发了这个应用程序,我还没有提出任何有关资源的错误,但考虑到目标PC的低配置,我想确定)



我的应用程序中有大约25个表单,其中7-8个包含DataGridViews。其他人只是用标签和文本框来填充输入或显示数据 - 没什么特别的。



因为这是我的第一个完整的应用程序,我不知道它将如何执行表现。我在某处看到,在.Net应用程序中,只要表单更接近就必须调用dispose()方法,否则垃圾不会被收集,而DataGridView也是如此。真的吗?因为在整个应用程序中我没有在任何地方使用Dispose。我应该担心吗?



如果需要Dispose(),我应该在何时何地拨打电话?在FormClosed事件?如果表单有DataGridView我应该首先处理它然后处理表单还是直接处理表单?此外,如果你有经验的程序员可以指出一些好的做法,使应用程序更少依赖资源,减少崩溃,那就太棒了。



我真的很感激你的时间。



谢谢。

Guys I have developed a ticket management system in C# windows forms application. Now this is the first time I have developed any application that might be used in real world (my school may implement it).

As my school is not that rich and mostly run by charity, so the technology they use is pretty old (Pentium 4 pcs with windows XP having maximum of 1 gb RAM), so I need to make my application as light-weight as possible i.e. it should not be resource hungry. (I have developed this app on dual core laptop with 4 gb RAM, and I haven't come up with any errors yet regarding to resources but considering the low config of target PC(s) I want to be sure)

I have about 25 forms in my application, in which 7-8 have DataGridViews in them. Others are just filled with labels and textboxes for taking input or display data - nothing fancy.

As this is my first complete application I am not sure how it will perform regarding the performance. I read somewhere that in .Net applications you have to call dispose() method whenever the form is closer or else the Garbage wont be collected and same goes for DataGridView. Is that true? because throughout the application I haven't used Dispose anywhere. Should I be worried?

If Dispose() is necessary, where and when should I call it? on FormClosed event? If a form has DataGridView should I dispose it first and then dispose form or is directly disposing the form enough? Also if you "experienced programmers" could point out some good practices to make application less resource dependent and less crash-prone then that would be great.

I really appreciate your time.

Thank you.

推荐答案

你应该在实现IDisposable的所有东西上使用Dispose - Form ,因为它继承自Control。



但是......你不应该在类实例中调用Dispose:你应该从外面做:

You should use Dispose on everything that implements IDisposable - Form does, because it inherits from Control.

But...you shouldn't call Dispose from inside the class instance: you should do it from outside:
MyForm mf = new MyForm();
if (mf.ShowDialog() == DialogResult.OK)
    {
    string path = mf.FilePath;
    ...
    }
mf.Dispose();

如果您尝试从对象FormClosed事件中处置对象,上面的代码将作为MyForm失败在你有机会从它的属性中读取路径之前,实例会被废弃。



我不会太担心数据网格视图等等形式 - 框架应该处理它,因为它构建它们,而不是你。





我明白了,但我不知道如果不是在形式关闭的事件中,我会得到如何在表格上调用处理。

这是我展示另一种表格的方式(点击按钮事件):



If you try to dispose objects from within the object FormClosed event, the above code would fail as the MyForm instance would have been Disposed before you got a chance to read the path from it's properties.

I wouldn't worry too much about data grid views and so forth within your forms - the framework should deal with that because it constructed them, not you.


"I see, but I don't get how to call dispose on forms if not in form closed event.
This is how I show another form (in click event of a button):"

Form12 frmExO = new Form12();
frmExO.FormClosed += new FormClosedEventHandler(childForm_FormClosed);
frmExO.Show();
this.Hide();





在childForm_FormClosed功能,我有:





"In childForm_FormClosed function, I have :"

this.Refresh();
this.Visible = true;

< br $>




好​​吧,你可以将dispose添加到FormClosed事件处理程序中(但是你必须断开事件处理程序以确保它正确释放实例)



或者......只需这样做:




Well, you could add the dispose to the FormClosed event handler (but you'd have to disconnect the event handler to be sure it freed up the instance properly)

Or...just do this:

using (Form12 frmExO = new Form12())
   {
   Hide();
   frmExO.ShowDialog();
   }
Show();

使用块的会自动为您调用Dispose,视觉效果也会相同。

The using block automatically calls Dispose for you and the visual effect will be the same.


理想情况下,Windows应用程序将有一个主窗体,它将创建和销毁其他窗体或用户控件,因为用户通过单击不同的选项来驱动应用程序。理想情况下,这个主表单应该在子表单或用户控件上调用dispose。理想情况下,用户控件无法关闭只能通过托管表单处理掉。



设计主窗体和子窗体或用户控件的方式对任何给定时间点的内存占用都有很大影响。在创建用户控件和子窗体时,确保您的主窗体尽可能轻。例如,您在主窗体中有3个选项卡,并且假设您使用用户控件在每个选项卡中托管,那么当用户切换到第二个选项卡时您可以创建第一个选项卡,您可以在选项卡更改事件中创建它们。我希望你能得到这张照片。同样的方式你也可以吝啬,你可以在交换机发生时从旧标签处理用户控件,但是如果它在控件中有未保存的数据,则可能会变得棘手,因为你可能必须强制用户保存它或将其临时保存在数据库或文件中用户切换回来时恢复。



您已经提到过使用7-8网格,这样可以优化网格数据的加载处理,或者您可以想到更好的表单设计将数据显示为托管如此多的网格以一种形式要求数据存在于内存中,用户很多不会同时查看所有网格。



如果您遵循上述建议,您的应用程序逻辑可能变得安静。您的用户工作流程将决定您的应用程序的设计。
Ideally Windows applications will have one main form which will create and destroy other forms or user controls as user drives the application by clicking on different options. This main form should ideally be calling dispose on child forms or user controls. Ideally user controls cannot be closed can only disposed off by hosting form.

The way you design main form and child forms or user control has great impact on memory footprint it has in any given point in time. Make sure your main form is light as possible be lazy while creating user controls and child forms. For example you have 3 tabs in your main form and let say your using a user control to host in each tab then you can create only first tab when application loads when user switches to second tab you can create them at tab change event. I hope you get the picture. Same way you can be stingy too, you can dispose the user controls from old tab as switch happens but if it has unsaved data in controls things can get tricky as you may have to force user to save it or persist it temporary in database or file and restore when user switches back.

You have mentioned that your using 7-8 grids so you can optimize their loading disposing of grid data to, or you can think of better form design to display data as hosting so many grid in one form requires that data to be present in memory, users many not look at all grids at the same time.

Your application logic can get quiet complex if you follow the above advice. Your user workflow will determine the design of your application.


这篇关于如何让我的C#windows应用程序轻量级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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