473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Disposing of Forms.

AP
If I display a form using the ShowDialog method, when the form is closed
should I have to explicitly call dispose on the form in order to ensure it
releases its resources, or should this be handled by the garbage collector
when the method that instantiates and opens the form goes out of scope?

Thanks,

Adam
Nov 15 '05 #1
9 1545
In any way it isn't error to dispose an object even you have a garbage
collector
"AP" <ad***@indra.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
If I display a form using the ShowDialog method, when the form is closed
should I have to explicitly call dispose on the form in order to ensure it
releases its resources, or should this be handled by the garbage collector
when the method that instantiates and opens the form goes out of scope?

Thanks,

Adam

Nov 15 '05 #2
AP,

If you are not going to use the dialog again (you might want to show it
again, and therefore keep it around), you should call Dispose on it. This
will release any resources that it is holding that you don't want to wait
for the GC to dispose of. This can consist of window handles (in this
case), or other unmanaged resources.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"AP" <ad***@indra.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
If I display a form using the ShowDialog method, when the form is closed
should I have to explicitly call dispose on the form in order to ensure it
releases its resources, or should this be handled by the garbage collector
when the method that instantiates and opens the form goes out of scope?

Thanks,

Adam

Nov 15 '05 #3
You should call Dispose on all modal dialogs as a matter of course.

See http://www.milbertus.com/archives/2004/02/003013.php for a little more
insight to this as well as some links to more in depth articles on the
issue...

--
Leit Rachsor
Nov 15 '05 #4
AP
I was under the impression that Dispose would get called automatically by
the garbage collector when the object goes out of scope. Is this not the
case?

Adam

"Leit Rachsor" <do**@email.me> wrote in message
news:e8*************@tk2msftngp13.phx.gbl...
You should call Dispose on all modal dialogs as a matter of course.

See http://www.milbertus.com/archives/2004/02/003013.php for a little more
insight to this as well as some links to more in depth articles on the
issue...

--
Leit Rachsor

Nov 15 '05 #5
Yes. Calling it yourself is a preventative measure and won't cause any
problems.

--
Leit Rachsor
Nov 15 '05 #6
AP
OK let me ask another question. Suppose I create a panel, and then in a
subclassed form pass in that panel to the constructor of the form, keeping a
handle on the panel outside of the form. I then show the form using
ShowDialog and call dispose on the form. I still have a handle on the panel
however. Do I have to call dispose on the panel also? Or will this be called
by the garbage collector when I no longer have a handle on the panel? And if
the garbage collector calls dispose for the panel, why do I have to
explicitly call it for a modal dialog?

Thanks,

Adam
"Leit Rachsor" <do**@email.me> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
Yes. Calling it yourself is a preventative measure and won't cause any
problems.

--
Leit Rachsor

Nov 15 '05 #7
AP <ad***@indra.com> wrote:
I was under the impression that Dispose would get called automatically by
the garbage collector when the object goes out of scope. Is this not the
case?


No, it's not. The garbage collector doesn't do anything when the
variable goes out of scope (objects themselves never go out of scope).
At some time after the variable has gone out of scope, the garbage
collector will collect the object if there's nothing else keeping it
alive, but it could be a long time before that happens.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
AP
I didn't necessarily mean immediately, just that at some point dispose will
be called automatically if there's nothing else keeping it alive, correct?
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
AP <ad***@indra.com> wrote:
I was under the impression that Dispose would get called automatically by the garbage collector when the object goes out of scope. Is this not the
case?


No, it's not. The garbage collector doesn't do anything when the
variable goes out of scope (objects themselves never go out of scope).
At some time after the variable has gone out of scope, the garbage
collector will collect the object if there's nothing else keeping it
alive, but it could be a long time before that happens.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #9
AP <ad***@indra.com> wrote:
I didn't necessarily mean immediately, just that at some point dispose will
be called automatically if there's nothing else keeping it alive, correct?


Correct - as long as the finalizer for the class calls Dispose. (Not
all classes which implement IDisposable have finalizers, or indeed need
them.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
4
by: Dakkar | last post by:
I have a program with windows forms and after execution of my program im making it invisible for working background progress and i have a dispose function like this protected override void...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
2
by: Dave | last post by:
I'm having trouble understanding dispose. I set up a class that, among other things, displays the time in a status bar panel. It does this by starting a thread. I create an instance of this...
4
by: Anthony Nystrom | last post by:
Anytime I close a form should I also dispose such as me.clos me.dispos What else Should I dispose... If I set a var to nothing should I also dispose such as f = nothin f.dispos
5
by: Chris | last post by:
I have a form that requires drawing custom lines on it. The color of the lines is suppose to be the same as the forcolor of the form. Am I doing this the most efficent and correct way? ...
3
by: Henry Jones | last post by:
I have a project that has 5 or 6 forms. VB.NET VS 2005 In the FormClosing Event of each form I have the following code: If Not IsNothing(frmA) Or Not frmA.IsDisposed Then frmA.Close() If...
4
by: Peter Webb | last post by:
I am supposed to manually dispose of some instances, such as Brushes, right? I have a couple of questions: 1. I have the following code, and it works just fine: ...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
2
by: libish | last post by:
hi all... i have an application... where i'm creating forms dynamically... once i created the forms what i'm doing is that, i'm calling formob.show(); here everything works perfect.. but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.