where to dispose of form when not shown modal?

M

Michael.Suarez

Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?
 
M

Matthias Pieroth

Hello,

you never need to dispose a Form, the garbage collector does it for you. You
can call Close() on the Form, that's it.

Bye
 
B

Bruce Wood

Suppose I have a button on a form that opens up another form. the code
in the buttons click event is:

frmMyCustomForm frm = new frmMyCustomForm ();

frm.ShowDialog();

frm.Dispose();

The form opens, user does what they need, the form closes, and gets
disposed immediately afterwards, due to the fact that the program is
stuck on the frm.ShowDialog() line until the form closess.

But suppose I don't want to do ShowDialog. I just want to use
frm.Show(). In that case, the program would continue to the next line
and I certainly wouldn't want to immediately dispose it. Where do I
dispose it?

A form shown using .Show() disposes of itself when the user closes it.
The only reason that forms shown using .ShowDialog() don't do this is
that they expect you may want to query them after the user presses OK
in order to get information out of them before they are disposed.

By the way, a useful pattern for dealing with forms that are to be
shown as dialogs is this:

using (frmMyCustomForm frm = new frmMyCustomForm ())
{
... set up dialog ...
frm.ShowDialog();
... get info back from dialog ...
}

Of course, if you're using .Show() this idiom isn't appropriate.
 
B

Bruce Wood

Matthias said:
Hello,

you never need to dispose a Form, the garbage collector does it for you. You
can call Close() on the Form, that's it.

Microsoft disagrees with you: see the explicit instructions to
Dispose() a Form shown using ShowDialog:

http://msdn2.microsoft.com/en-us/library/c7ykbedk.aspx

You are, however, correct when a Form is shown using Show(): the Form
disposes of itself upon closing.

I believe (I could be wrong) that the issue here is when the window
handle and its associated resources are released back to the operating
system. The Dispose() method releases the Form's real O/S resources
without waiting for the GC. Since there is no guarantee as to when the
GC is going to run, it is preferable to Dispose() and release any O/S
resources used by the controls and the window sooner rather than later.

Although, it is true, the GC will eventually perform the same function
if you forget / don't bother.
 
M

Michael.Suarez

Great info!

Thanks to all who replyed!

Bruce said:
A form shown using .Show() disposes of itself when the user closes it.
The only reason that forms shown using .ShowDialog() don't do this is
that they expect you may want to query them after the user presses OK
in order to get information out of them before they are disposed.

By the way, a useful pattern for dealing with forms that are to be
shown as dialogs is this:

using (frmMyCustomForm frm = new frmMyCustomForm ())
{
... set up dialog ...
frm.ShowDialog();
... get info back from dialog ...
}

Of course, if you're using .Show() this idiom isn't appropriate.
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

That is not correct.

When a form is closed (receives the WM_CLOSE windows message) the
implementation of Dispose is ultimately called.

That being said, it would be fine to handle unmanaged resources in your
form and override the Dispose method to properly take care of your unmanaged
resources.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top