MyDlg dlg = new MyDlg();
dlg.ShowDialog();
after the dialog is closed here I expected that Form.Dispose would be called
but this isn't the case.
Why? After closing the application, Dispose was called.
I had a very funny bug because of this: I run a System.Windows.Forms.Timer
in this dialog and expected it would be stopped & disposed after the dialog
was closed but this was not the case. It continued running after the dialog
was closed and at the end I had multiple instances of this dialog in memory
with their timers running but neither of them was visible and they did
funny things in my app.
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Jon Skeet [C# MVP] - 23 Jul 2004 14:39 GMT
> MyDlg dlg = new MyDlg();
> dlg.ShowDialog();
[quoted text clipped - 9 lines]
> with their timers running but neither of them was visible and they did
> funny things in my app.
Well, the documentation does talk about this:
<quote>
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.
</quote>

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Stoitcho Goutsev \(100\) [C# MVP] - 23 Jul 2004 14:54 GMT
Hi cody,
When dilaog box (form shown via ShowDialog) is closed it hides the form
rather than dispose it. That's because it is more likely that one will want
to read some of the forms control values, etc.
When a form is show as modaless, though, calling close dipose the form.
If you want your dialog disposed you can use the following
using(SomeDialogForm dlg = new DialogForm())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
//Process the dialog data
}
}

Signature
HTH
Stoitcho Goutsev (100) [C# MVP]
> MyDlg dlg = new MyDlg();
> dlg.ShowDialog();
[quoted text clipped - 15 lines]
> Freeware Tools, Games and Humour
> http://www.deutronium.de.vu || http://www.deutronium.tk
cody - 23 Jul 2004 15:49 GMT
Thank you both. That makes sense.
But is it correct to say that it is not very important in a WinForms based
application to call dispose after the dialog has finished because the
finalizer is called in the next but one garbage collection anyway?
Another problem arises because GC doesn't call dispose, but instead
~MyDlg(), so all code I write into the Dispose method will not invoked.
--
cody
Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
> MyDlg dlg = new MyDlg();
> dlg.ShowDialog();
[quoted text clipped - 15 lines]
> Freeware Tools, Games and Humour
> http://www.deutronium.de.vu || http://www.deutronium.tk
Jay B. Harlow [MVP - Outlook] - 23 Jul 2004 16:09 GMT
Cody,
> But is it correct to say that it is not very important in a WinForms based
> application to call dispose after the dialog has finished because the
> finalizer is called in the next but one garbage collection anyway?
That is why it is VERY important to call Dispose on a dialog!
You have no idea how far away that garbage collection is, it could be
several minutes! You are holding onto valuable unmanaged resources (Win32
window handles) that should be released "asap", hence you should call
Dispose on your dialog "asap".
Hope this helps
Jay
> Thank you both. That makes sense.
>
[quoted text clipped - 33 lines]
> > Freeware Tools, Games and Humour
> > http://www.deutronium.de.vu || http://www.deutronium.tk
Stoitcho Goutsev \(100\) [C# MVP] - 23 Jul 2004 19:00 GMT
Hi cody,
The component's finalizer (~Component in C#) calls Dispose. Hence your
dispose implementation is going to be called by the GC

Signature
Stoitcho Goutsev (100) [C# MVP]
> Thank you both. That makes sense.
>
[quoted text clipped - 33 lines]
> > Freeware Tools, Games and Humour
> > http://www.deutronium.de.vu || http://www.deutronium.tk