Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 1-Apr-14 2:08am
v6
Comments
[no name] 1-Apr-14 7:38am    
Don't worry about things unless you can demonstrate that a real problem exists.

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:
C#
MyForm mf = new MyForm();
if (mf.ShowDialog() == DialogResult.OK)
    {
    string path = mf.FilePath;
    ...
    }
mf.Dispose();
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):"

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


"In childForm_FormClosed function, I have :"

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



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:
C#
using (Form12 frmExO = new Form12())
   {
   Hide();
   frmExO.ShowDialog();
   }
Show();
The using block automatically calls Dispose for you and the visual effect will be the same.
 
Share this answer
 
v2
Comments
Shajee Afzal 1-Apr-14 9:25am    
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();

In childForm_FormClosed function, I have :

this.Refresh();
this.Visible = true;
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900