473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

form disposal


I have a method that creates of new form each time its called do i need
to dipose of the form when it closes or doe the garbage collector take care
of it?

code
private void button1_Click(object sender, EventArgs e)

{

frmImportData ImportData = new frmImportData();

ImportData.ShowDialog(); // when ImportData form closes do i need to
dispose?

}

rgds,Steve
Oct 20 '06 #1
7 2616

I wouldn't call Dispose here. It should have been disposed by calling
Close in ImportData's code, or by click the X button.

Steve wrote:
I have a method that creates of new form each time its called do i need
to dipose of the form when it closes or doe the garbage collector take care
of it?

code
private void button1_Click(object sender, EventArgs e)

{

frmImportData ImportData = new frmImportData();

ImportData.ShowDialog(); // when ImportData form closes do i need to
dispose?

}

rgds,Steve
Oct 20 '06 #2
Oddly enough, I always would... even though it perhaps should have been
done already; I find that the following default works well for
disposable items:

1: is it disposable?
2: do I feel ownership? (i.e. did I create / adopt it in this code
block)
3: is the scope contained? (i.e. it doesn't logically live beyound this
region, as non-modal forms would)

If I can answer "yes" to these, then I use "using", and this tends to
be the majority; so here I would:

using(SomeForm formInstance = new SomeForm()) {
//[*] note for later
formInstance.ShowDialog();
}

OK, in reality there isn't enough going on here that it is an issue,
but! Now imagine that there are a few lines of code at point[*]; now
it becomes less clear whether it is "clean" when abandoned through an
exception. It is /probably/ the case Dispose() mainly deals with
"hwnd"s, in which case they *probably* won't have even been issues
until .Show() / .ShowDialog(), but that is using inside knowledge of a
class, which defeats the purpose of the encapsualtion. Besides, I'm
sure I could create a Form class which calls CreateHandle() earlier
than normal, or obtains it's own disposable resources in the ctor.

The other advantage is this approach it works in all normal scenarios,
and saves you from having to ask "should I do x / y / z", which you can
only really answer by knowing too much about the internals of the class
in question. Which could change at any point. And yes, finalizers are
there to a point, but I always prefer deterministic disposal of
unmanaged resources.

Just a counterview...

Marc

Oct 20 '06 #3
Hi Everyone,

Furthermore, in the .NET 2.0 Framework, closing a Form by way of code, e.g.,
aForm.Close(), or by the clicking the close button, does not make a call to
Dispose if the Form is shown using the ShowDialog method.

All Forms opened with ShowDialog should be explicitly disposed, not only
because they are IDisposable, as Marc pointed out, but also because they won't
be disposed until finalization otherwise (AFAIK).

There is one major reason for this behavior. Modal dialogs are meant in many
cases to return a value to the "opener", such as the DialogResult and choices
that the user has made. Disposing the Form automatically could be problematic
in these cases.

--
Dave Sexton

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Oddly enough, I always would... even though it perhaps should have been
done already; I find that the following default works well for
disposable items:

1: is it disposable?
2: do I feel ownership? (i.e. did I create / adopt it in this code
block)
3: is the scope contained? (i.e. it doesn't logically live beyound this
region, as non-modal forms would)

If I can answer "yes" to these, then I use "using", and this tends to
be the majority; so here I would:

using(SomeForm formInstance = new SomeForm()) {
//[*] note for later
formInstance.ShowDialog();
}

OK, in reality there isn't enough going on here that it is an issue,
but! Now imagine that there are a few lines of code at point[*]; now
it becomes less clear whether it is "clean" when abandoned through an
exception. It is /probably/ the case Dispose() mainly deals with
"hwnd"s, in which case they *probably* won't have even been issues
until .Show() / .ShowDialog(), but that is using inside knowledge of a
class, which defeats the purpose of the encapsualtion. Besides, I'm
sure I could create a Form class which calls CreateHandle() earlier
than normal, or obtains it's own disposable resources in the ctor.

The other advantage is this approach it works in all normal scenarios,
and saves you from having to ask "should I do x / y / z", which you can
only really answer by knowing too much about the internals of the class
in question. Which could change at any point. And yes, finalizers are
there to a point, but I always prefer deterministic disposal of
unmanaged resources.

Just a counterview...

Marc

Oct 20 '06 #4
In this case Dispose() is necessary, as documented
(http://msdn2.microsoft.com/en-us/lib...w58wzka.aspx):

"
Dispose will be called automatically if the form is shown using the Show
method. If another method such as ShowDialog is used, or the form is never
shown at all, you must call Dispose yourself within your application.
"

If the code is not performance critical, make Dispose() a habit. Use it
whenever it is possibile, even when it could be unnecessary. It does not
hurt, but it can (will) help.
"Truong Hong Thi" <th*****@gmail.comha scritto nel messaggio
news:11**********************@m7g2000cwm.googlegro ups.com...
>
I wouldn't call Dispose here. It should have been disposed by calling
Close in ImportData's code, or by click the X button.

Steve wrote:
>I have a method that creates of new form each time its called do i need
to dipose of the form when it closes or doe the garbage collector take
care
of it?

code
private void button1_Click(object sender, EventArgs e)

{

frmImportData ImportData = new frmImportData();

ImportData.ShowDialog(); // when ImportData form closes do i need to
dispose?

}

rgds,Steve

Oct 20 '06 #5
PS
"Steve" <cc******@bigpond.net.auwrote in message
news:So*******************@news-server.bigpond.net.au...
>
I have a method that creates of new form each time its called do i need
to dipose of the form when it closes or doe the garbage collector take
care of it?
The using statement is what I use.

using (frmImportData ImportData = new frmImportData())
{
ImportData.ShowDialog();
}

PS

Oct 20 '06 #6
Furthermore, in the .NET 2.0 Framework, closing a Form by way of code, e.g.,
aForm.Close(), or by the clicking the close button, does not make a call to
Dispose if the Form is shown using the ShowDialog method.
Just look at MSDN for Form.Close, it does dispose the dialog. There is
only one exception:
"The one condition when a form is not disposed on Close is when it is
part of a multiple-document interface (MDI) application, and the form
is not visible."

For disposable objects, it is largely understood that Close is just
another name for Dispose in the cases when the verb "Close" is more
appropriate. It would be a trap if Close means something else.

However, when X button of a modal dialog is clicked, the form is
hidden, not closed, and DialogResult is set to Cancel.

Regards,
Thi

Oct 21 '06 #7
Hi Truong,

You are incorrect. When a Form shown by ShowDialog is closed by clicking the
close button with your mouse, the Close method is called, but not Dispose.
The Closing and Closed events are raised as well.

--
Dave Sexton

"Truong Hong Thi" <th*****@gmail.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
>Furthermore, in the .NET 2.0 Framework, closing a Form by way of code,
e.g.,
aForm.Close(), or by the clicking the close button, does not make a call to
Dispose if the Form is shown using the ShowDialog method.

Just look at MSDN for Form.Close, it does dispose the dialog. There is
only one exception:
"The one condition when a form is not disposed on Close is when it is
part of a multiple-document interface (MDI) application, and the form
is not visible."

For disposable objects, it is largely understood that Close is just
another name for Dispose in the cases when the verb "Close" is more
appropriate. It would be a trap if Close means something else.

However, when X button of a modal dialog is clicked, the form is
hidden, not closed, and DialogResult is set to Cancel.

Regards,
Thi

Oct 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
6
by: Matt84121 | last post by:
I have a really stupid question, and cannot find the answer elsewhere. I have a mdi that uses a class. One (main) form has two textboxes which takes input, and the input is calculated by the class...
6
by: Robert Dufour | last post by:
On my form if the user clicks the upper right hand corner to close the form I want to trap that event and do a check to see if closing is allowed, if not, I want to stop the form closing action. ...
5
by: M Skabialka | last post by:
I am creating my first Visual Studio project, an inventory database. I have created a form and used written directions to add data from a table to the form using table adapters, data sets, etc. ...
3
Corster
by: Corster | last post by:
I'm fairly new to Access, so please have patience... I have an application that I've written to audit all of our computers, monitors etc. which also works-out the Intel processor details from the...
2
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference...
1
by: scgamecock | last post by:
I am trying to build a search form that will display the data if the data exist and if the disposal check box is checked. I would like for an error message to display if the data exist, but the...
3
by: dirk | last post by:
Hi, Being new coding in phpI have a question concerning server side form validation. In a php script I check if a form is correctly filled in. Now I want that the page containing the forms...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.