please help! Problem freeing resources

  • Thread starter Tilfried Weissenberger
  • Start date
T

Tilfried Weissenberger

Hi,

I have a strange problem. I have a (self-made) Calendar object on a
form which has a timer object to update itself continually from the
database. Upon closing of the Form, I see in the debug-output that the
calendar continues to poll data from the calendar.

Similarly I have a Form with just a DataGrid, the first time it is
opened, it takes longer (the Design of the DataGrid takes that long).
If I close that window and re-open it, it's really fast - which leads
me to believe that the resources are aswell not released!

My questions are

1) how do I _really_ close a Form/Window?
2) Why isn't the Form.Close() function enough?
3) Why won't Form.Dispose() release all resources used?
4) Assuming Sql-Server and connection-pooling, should I call Close()
or Dispose() on a no longer needed SqlConnection, or do I have to at
all, if the variable looses scope right afterwards anyways?

thanks for any enlightment on these matters!

regards, Tilli
 
J

Jeff Ptak

Hey there...maybe I can shed some light on your questions:
1) how do I _really_ close a Form/Window?
You simply call Form.Close and then Form.Dispose() if you are done with it.
2) Why isn't the Form.Close() function enough?
What you should do is handle the form's Closing event and shut down your
timer and dispose of it.
3) Why won't Form.Dispose() release all resources used?
It does release resources that the Form class creates, but you are
responsible for releasing resources created by you. It is always good
practice to destroy the objects you create instead of relying on them going
out of scope. You cannot be sure when the .NET Framework's garbage
collector is going to run.
4) Assuming Sql-Server and connection-pooling, should I call Close()
or Dispose() on a no longer needed SqlConnection, or do I have to at
all, if the variable looses scope right afterwards anyways?

You should ALWAYS call the Close method when you are done with a connection.
It is also good practice to call the dispose method when you are sure you
are done with an object.

To answer your question about why a form takes longer the 1st time, I
believe the reason is that the 1st time a form is requested it is compiled
"just in time". Subsequent calls to that form use the compiled version
which is much faster. (That may just be for Web Forms, but I think it
applies to Win Forms as well.)

Hope that helps a little.
 
T

Tilfried Weissenberger

Hi Jeff!
Hey there...maybe I can shed some light on your questions:
Yes, indeed, thanks!

One thing I found is that Windows.Forms.Form- and
Windows.Forms.UserControl-Objects both create (through VS2k3) a
private System.ComponentModel.Container components = null;
collection
and clean that during the auto-created Dispose() function. But, there
is no code whatsoever which fills this Container with controls!

Is that left to me or do I just disregard this "components" collection
alltogether?

thanks again!

regards, Tilli
 
T

Tilfried Weissenberger

Hi again, Jeff,

Following Scenario:

I have a Windows.Forms.Form - a user clicks the inherited close-window
icon on the title-bar of that form to close it.

1) That already calls Form.Close() - doesn't it?
2) Where should I call Form.Dispose()?
3) Now if I want to add a "Save changes first?" question into the
Form_Closing() event, I noticed that some of the controls (TextBox for
example) no longer serve the entered value in the .Text attribute! So
I cannot save the changes from within this event!?!

thanks!

regards, Tilli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top