What is proper method for Closing a Windows Form

S

spebola

I am using VB.Net 2003 professional edition, and .Net Framework 1.1,
and Windows forms.

What is the proper way to close a windows form displayed by the Show
method? I would like it to be removed from memory. Is there any
difference for the modal method(ShowDialog)? I have experimented with
me.Close and me.Dispose and can not detect a difference. I thought
the me.dispose method did all that me.close did plus set the form
object to nothing.

I have a hundred or so forms in my application, and when I move around
from form to form, (closing the previous form) my application grows
larger and larger according to the task manager. Does the garbage
collector only run when all memory is used?

Any comments would be appreciated.
 
C

CJ Taylor

Wow... 100+ forms? Thats a big one. =)

Dispose calls the garbage collector and starts clearing memory. Close does
not destroy the instance.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (spebola) scripsit:
I am using VB.Net 2003 professional edition, and .Net Framework 1.1,
and Windows forms.

What is the proper way to close a windows form displayed by the Show
method? I would like it to be removed from memory. Is there any
difference for the modal method(ShowDialog)? I have experimented with
me.Close and me.Dispose and can not detect a difference. I thought
the me.dispose method did all that me.close did plus set the form
object to nothing.

MSDN on the form's 'Close' method:

<msdn>
When a form is closed, all resources created within the object are
closed and the form is disposed.
</msdn>
 
C

CJ Taylor

Really? Heh, this book was wrong....

=)


Herfried K. Wagner said:
* (e-mail address removed) (spebola) scripsit:

MSDN on the form's 'Close' method:

<msdn>
When a form is closed, all resources created within the object are
closed and the form is disposed.
</msdn>
 
A

anonymous

From these resonses I still do not know how to close the form properly.
Monitoring the task manager as I move from form to form the application
keeps getting larger, memory wise. I am closing a form before I move to
the next one. only the "startup" form is always open. I can detect no
difference in memory usage if I close a form using me.Close or
me.Dispose. If me.Dispose is setting the form object to nothing and
therefore all controls, dataset, etc. in the form to nothing, seems like
some memory would be released. Maybe the GC is not running until the
system needs memory.
 
P

Peter Huang

Hi,

As the MSDN said,
When a form is closed, all resources created within the object are closed
and the form is disposed.

Disposes of the resources (other than memory) used by the Form.

Based on my research, I test the code below on .NET framework 1.1 VS.NET
2003.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fm As Form2
fm = New Form2
fm.Show()
fm.Close()
End Sub

after the you click button1, the Form2's resource will be disposed. If you
call the code line below
GC.Collect()
GC.WaitForPendingFinalizers()
The Form2 will be collected by GC.

As for the memory is not released, I think this is for performance concern,
so it was in the cache.
You may try to write a program to open a form ,close it and GC.collect it
, again and again. You will find that the memory usage will not increase
all the time.

If you have any concern on this issue,please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Form Lost Focus 4
Forms: Close Dialog weird problem 2
Multiple Form Issue - Closing Forms 2
Singleton Form 1
Closing a form 2
Windows Form Application Help 2
"Closing" a Form 5
Form Closing VB.NET VS2005 3

Top