Re: Dispose(bool), Idisposable, form closing etc.
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 19 Nov 2007 15:32:20 -0500
Ray,
See inline:
1. The Dispose(Bool) the IDE generates for a form has nothing to do
with IDisposable, right?
Well, in a roundabout way, it does. The implementation of the Dispose
method on the form class Form will call the override that is defined in your
subclass (the Dispose(bool) implementation).
2. So when is this called? When a form is closed? If this is caused
automatically when a form is closed how can I then access things on a
modal from after ShowDialog() returns? Or is it only for modeless
forms?
It is called when a form is closed and it is not a modal form, yes.
When showing the form modally, it doesn't dispose the form until Dispose is
called on it (if you click the X button, it doesn't dispose yet, it just
hides it). From the documentation from the ShowDialog method:
When a form is displayed as a modal dialog box, clicking the Close button
(the button with an X at the upper-right corner of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
Unlike modeless forms, the Close method is not called by the .NET Framework
when the user clicks the close form button of a dialog box or sets the value
of the DialogResult property. Instead the form is hidden and can be shown
again without creating a new instance of the dialog box. Because a form
displayed as a dialog box is not closed, you must call the Dispose method of
the form when the form is no longer needed by your application.
In this case, you would do something like this:
using (MyForm myForm = new MyForm())
{
// Show it.
if (myForm.ShowDialog() = <whatever you want to compare to>)
...
}
3. Does the garbage collector automically call Dispose() which is part
of IDisposable when an onbject is garbage collected? I know this is
non-deterministic etc. Is this what is known as the Finalizer or
whatever it's called?
Well, if Dispose is not called on the form (directly or indirectly) then
when the form has no more references to it, and it is garbage collected,
Dispose is called.
4. I don't understand how garbage collection works with forms. If I
have a method which declares a variable which is a form, and
instantiate it in the same routine, when I leave the routine the
variable is released, the form's reference count should then be zero
and therefore a candiate for garbage collection, no? Is there another
refernce to it somewhere?
There is no actual reference count, per se, but all the reference to the
form are gone, so yes, it is a candidate for GC. This assumes you didn't
pass the reference outside of the method, or assigned it to some field
elsewhere which is keeping the object alive.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
Confused,
Ray
.
- Follow-Ups:
- Re: Dispose(bool), Idisposable, form closing etc.
- From: rbrowning1958
- Re: Dispose(bool), Idisposable, form closing etc.
- References:
- Dispose(bool), Idisposable, form closing etc.
- From: rbrowning1958
- Dispose(bool), Idisposable, form closing etc.
- Prev by Date: Re: VS2008 - RTM'd
- Next by Date: Re: what is the best datatype for..
- Previous by thread: Re: Dispose(bool), Idisposable, form closing etc.
- Next by thread: Re: Dispose(bool), Idisposable, form closing etc.
- Index(es):