Re: Dispose(bool), Idisposable, form closing etc.
- From: rbrowning1958 <RBrowning1958@xxxxxxxxx>
- Date: Tue, 20 Nov 2007 03:21:35 -0800 (PST)
On 19 Nov, 20:32, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Ray,
See inline:
1. TheDispose(Bool) the IDE generates for a form has nothing to do
with IDisposable, right?
Well, in a roundabout way, it does. The implementation of theDispose
method on the form class Form will call the override that is defined in your
subclass (theDispose(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'tdisposethe form untilDisposeis
called on it (if you click the X button, it doesn'tdisposeyet, 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 theDisposemethod 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 callDispose() 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, ifDisposeis not called on the form (directly or indirectly) then
when the form has no more references to it, and it is garbage collected,Disposeis 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]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx
Confused,
Ray- Hide quoted text -
- Show quoted text -
Thank you all for replying I've arbitrarily chosen yours to reply to,
Nicholas. So let me see if I have this right:
1. Dispose(Bool) as part of a form is not explicitly part of the
IDisposable interface, but the implementation of IDisposable,Dispose
further up the class hierarchy (form I suppose) calls this. This is
waht Brian called the Canonical implementation (I never did understand
what that word meant!).
2, When a modeless form is called, clicking the form's close button,
or calling the close() method calls IDisposable.Dispose which in turn
calls Dispose(bool).
3. If it's a modal form, clicking the close button or calling the
close method just hides the form, and does not call
IDisposable.dispose. Therefore I have to do it myself or use a Using
clause.
4. A form's destructor will call Dispose(bool) although in most cases
it will not do much because it will already have been called by
IDisposable.dispose. SO...I'm guessing that Idisposable calls
dispose(true), whereas the destructor calls Dispose(false)?
5. With regard to garbage collecting - doesn't Peter have it correct
where he says the form is not garbage collected because it is
referenced by application.openForms?
6. Brian mentioned that some class authors write property getters to
return data even after the form has been destroyed - I don't
understand this. Doesn't this mean the property getter has to be
static - and where would it savbe the data? In static class vars? That
implies I only have one instance of the form, right?
Thanks everbody,
Best
Ray
.
- Follow-Ups:
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Brian Gideon
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Michael C
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Christof Nordiek
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Dispose(bool), Idisposable, form closing etc.
- References:
- Dispose(bool), Idisposable, form closing etc.
- From: rbrowning1958
- Re: Dispose(bool), Idisposable, form closing etc.
- From: Nicholas Paldino [.NET/C# MVP]
- Dispose(bool), Idisposable, form closing etc.
- Prev by Date: Re: Order or DbLinq properties in code generated by pgsqlmetal
- Next by Date: Re: Visual Studio 2008 released
- Previous by thread: Re: Dispose(bool), Idisposable, form closing etc.
- Next by thread: Re: Dispose(bool), Idisposable, form closing etc.
- Index(es):
Relevant Pages
|