By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

What is proper method for Closing a Windows Form

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Wow... 100+ forms? Thats a big one. =)

Dispose calls the garbage collector and starts clearing memory. Close does
not destroy the instance.
"spebola" <sp*****@swbell.net> wrote in message
news:b8*************************@posting.google.co m...
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.

Nov 20 '05 #2

P: n/a
* sp*****@swbell.net (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>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
Really? Heh, this book was wrong....

=)
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bq*************@ID-208219.news.uni-berlin.de...
* sp*****@swbell.net (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>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #4

P: n/a
* "CJ Taylor" <no****@blowgoats.com> scripsit:
Really? Heh, this book was wrong....


Maybe the docs are wrong...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
I'm not doubting you, just find the inconsitancy funny. =)
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bq*************@ID-208219.news.uni-berlin.de...
* "CJ Taylor" <no****@blowgoats.com> scripsit:
Really? Heh, this book was wrong....


Maybe the docs are wrong...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #6

P: n/a
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7

P: n/a
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.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.