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 / .NET Framework / Compact Framework / December 2007

Tip: Looking for answers? Try searching our database.

do we need to dispose screen controls?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Batterhead - 21 Dec 2007 11:07 GMT
hi there,

i use vs2005, c# and .netcf2. i know that i have to explicitly dispose
any controls and components which created by myself. but how about
those controls, e.g. button, which drew by c# on the screen. as a good
practice, should i dispose them explicitly? or c# already does it for
me with the system generated code? where is the code?

i know they will be handled eventually by GC but it does not
guaranteed.

thanks
batterhead
<ctacke/> - 21 Dec 2007 14:55 GMT
If they are part of the Controls collection in a Form, then the Form will
dispose of them for you.

Signature

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com

> hi there,
>
[quoted text clipped - 9 lines]
> thanks
> batterhead
Simon Hart [MVP] - 21 Dec 2007 22:07 GMT
Just ensure you call dispose on your form. It is good practice to use the
using statement. ie: in C#:

using (MyForm form = new MyForm())
{
   form.ShowDialog();
}

When control leaves the above block, the CLR will call Dispose on that form.
When creating a form via the Wizard, it will override the Dispose method of
the Component class and call Dispose on this for you. It overrides to clean
up the components object and so you can place stuff in there if you want.

You can find this in the designer partial class. ie MyForm.designer.cs.
Signature

Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com

> hi there,
>
[quoted text clipped - 9 lines]
> thanks
> batterhead
Hilton - 22 Dec 2007 07:54 GMT
Simon Hart wrote:
> Just ensure you call dispose on your form. It is good practice to use the
> using statement. ie: in C#:
[quoted text clipped - 3 lines]
>    form.ShowDialog();
> }

This doesn't compile, at least for me anyway (VS2003)

using (Form form = new Form ())
{}

Also, "(new Form () is IDisposable)" is false.

What am I doing wrong?

Hilton
<ctacke/> - 22 Dec 2007 13:18 GMT
IIRC, The using keyword isn't available until CF 2.0 (Studio '05).

Signature

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com

> Simon Hart wrote:
>> Just ensure you call dispose on your form. It is good practice to use the
[quoted text clipped - 15 lines]
>
> Hilton
Hilton - 22 Dec 2007 22:54 GMT
Chris,

"using" is definitely a 1.0 keyword.  I use it all the time; e.g. "using
(StreamReader sr = new StreamReader (...))".  The problem is that Form does
not implement IDisposable on CF 1.  Now if Form all of a sudden does
implement IDisposable, then that is, by Microsoft's definition, "a breaking
change" and should not be done.

"(new Form () is IDisposable) return false on CF 1.0, but true on .NET
(desktop).

Interesting...  Seems like a major screwup/oversight by Microsoft.  Yes?

That's why I still think that the GC should have: "if o implements
IDisposable && (!o.Disposed) then o.Dispose"  (pseudo code).  But I was shot
down last time and I guess I will be again.  So, Form does not implement
IDisposable on the Compact Framework - solve that.  If it is in CF 2.0, that
is a breaking change and should not be done (according to Microsoft).

Comments?

Hilton

> IIRC, The using keyword isn't available until CF 2.0 (Studio '05).
>
[quoted text clipped - 18 lines]
>>
>> Hilton
<ctacke/> - 22 Dec 2007 23:38 GMT
I just checked CF 1.0, 2.0 and 3.5 with Reflector and a Form is definitely
derived from IDisposable in all three.

Form->ContainerControl->ScrollableControl->Control->Component->IComponent->IDisposable

Signature

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com

> Chris,
>
[quoted text clipped - 42 lines]
>>>
>>> Hilton
Hilton - 23 Dec 2007 06:02 GMT
Chris,

>I just checked CF 1.0, 2.0 and 3.5 with Reflector and a Form is definitely
>derived from IDisposable in all three.
>
> Form->ContainerControl->ScrollableControl->Control->Component->IComponent->IDisposable

OK, so:

Q1: Why does:

using (Form form = new Form()) {}

give the following compiler error:

c:\test\FormDispose\Form1.cs(32): Cannot implicitly convert type
'System.Windows.Forms.Form' to 'System.IDisposable'

...and..

Q2: Why does: "(new Form () is IDisposable)" return false?

Q1 is a compile time check, Q2 is a run-time and they're consistent.

Hilton
Hilton - 23 Dec 2007 06:45 GMT
To follow-up my own post:

When I run (FormDispose.exe) with the line:

"MessageBox.Show (((new Form () is IDisposable).ToString()));"

it returns *false*.  This is using CF 1.

Then I add the file FormDispose.exe.config so that FormDispose uses CF 2 and
guess what?  Yip, it returns *true*.

Believe it, or not...

Hilton

> Chris,
>
[quoted text clipped - 21 lines]
>
> Hilton
<ctacke/> - 24 Dec 2007 17:04 GMT
Well you have to believe the runtime.  I can only conclude that I'm looking
at the wrong 1.0 binary (or at least not the one your system is using).
What version of 1.0 are you running?

Signature

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com

> To follow-up my own post:
>
[quoted text clipped - 36 lines]
>>
>> Hilton
Hilton - 25 Dec 2007 05:23 GMT
CF 1.0.4292.0 which is CF1 SP3

Try it, create a Pocket PC, add the line below and on CF 1, it shows false
(and using doesn't compile either).

Hilton

> Well you have to believe the runtime.  I can only conclude that I'm
> looking at the wrong 1.0 binary (or at least not the one your system is
[quoted text clipped - 40 lines]
>>>
>>> Hilton

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.