HighTechTalks DotNet Forums  

do we need to dispose screen controls?

Dotnet Framework (Compact Framework) microsoft.public.dotnet.framework.compactframework


Discuss do we need to dispose screen controls? in the Dotnet Framework (Compact Framework) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Batterhead
 
Posts: n/a

Default do we need to dispose screen controls? - 12-21-2007 , 06:07 AM






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

Reply With Quote
  #2  
Old   
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-21-2007 , 09:55 AM






If they are part of the Controls collection in a Form, then the Form will
dispose of them for you.


--

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


"Batterhead" <batterheadccw (AT) gmail (DOT) com> wrote

Quote:
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



Reply With Quote
  #3  
Old   
Simon Hart [MVP]
 
Posts: n/a

Default RE: do we need to dispose screen controls? - 12-21-2007 , 05:07 PM



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.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"Batterhead" wrote:

Quote:
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


Reply With Quote
  #4  
Old   
Hilton
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-22-2007 , 02:54 AM



Simon Hart wrote:
Quote:
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();
}
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




Reply With Quote
  #5  
Old   
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-22-2007 , 08:18 AM



IIRC, The using keyword isn't available until CF 2.0 (Studio '05).


--

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




"Hilton" <nospam (AT) nospam (DOT) com> wrote

Quote:
Simon Hart wrote:
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();
}

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





Reply With Quote
  #6  
Old   
Hilton
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-22-2007 , 05:54 PM



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



"<ctacke/>" <ctacke[at]opennetcf[dot]com> wrote

Quote:
IIRC, The using keyword isn't available until CF 2.0 (Studio '05).


--

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




"Hilton" <nospam (AT) nospam (DOT) com> wrote in message
news:hp3bj.32302$JD.4986 (AT) newssvr21 (DOT) news.prodigy.net...
Simon Hart wrote:
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();
}

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







Reply With Quote
  #7  
Old   
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-22-2007 , 06:38 PM



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


--

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




"Hilton" <nospam (AT) nospam (DOT) com> wrote

Quote:
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



"<ctacke/>" <ctacke[at]opennetcf[dot]com> wrote in message
news:Ozvrp0JRIHA.2376 (AT) TK2MSFTNGP02 (DOT) phx.gbl...
IIRC, The using keyword isn't available until CF 2.0 (Studio '05).


--

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




"Hilton" <nospam (AT) nospam (DOT) com> wrote in message
news:hp3bj.32302$JD.4986 (AT) newssvr21 (DOT) news.prodigy.net...
Simon Hart wrote:
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();
}

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









Reply With Quote
  #8  
Old   
Hilton
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-23-2007 , 01:02 AM



Chris,

Quote:
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




Reply With Quote
  #9  
Old   
Hilton
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-23-2007 , 01:45 AM



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



"Hilton" <nospam (AT) nospam (DOT) com> wrote

Quote:
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





Reply With Quote
  #10  
Old   
 
Posts: n/a

Default Re: do we need to dispose screen controls? - 12-24-2007 , 12:04 PM



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?


--

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




"Hilton" <nospam (AT) nospam (DOT) com> wrote

Quote:
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



"Hilton" <nospam (AT) nospam (DOT) com> wrote in message
news:IRmbj.784$6%.53 (AT) nlpi061 (DOT) nbdc.sbc.com...
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







Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.