Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2004

Tip: Looking for answers? Try searching our database.

Why Form.Dispose isn't called?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cody - 23 Jul 2004 14:17 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.