Form.Dispose method

M

Max2006

Hi,



Considering the fact that Form.Dispose method is implemented in the Designer
partial class, what would be the best way to have my own Dispose logic?



I assume that I should not modify the designer code and I am not sure how to
implement a Dispose method for my form.



Thank you,

Max
 
R

RobinS

Why do you need to? Does it have some kind of unmanaged code in it,
or some kind of COM object? When your form is closed, it will be
automatically disposed, assume nothing else has a reference open to it.

Robin S.
 
L

Linda Liu [MSFT]

Hi Max,

It seems that you're using VS2005.

In VS2005, the override Dispose(bool) method implementation and Windows
Forms Designer generated code, i.e. the InitializeComponent method are
separated into a designer file.

It is not recommended to change the code in the InitializeComponent method
because the designer would change the code in the method when there's a
change on the form designer.

But it doesn't include the Dispose(bool) method. We could write our own
code in the Dispose(bool) method without any worry about our own code's
being flushed by Windows Forms Designer later.

The Form class has implemented IDisposable interface and define a virtual
method Dispose(bool). The virtual Dispose(bool) method contains the code to
release resources the form is holding, and is called in the
IDisposable.Dispose implementation within the Form class with a parameter
of value true. The destructor of the form will also call the Dispose(bool)
method, but with a parameter of value false. FYI, when the form is closed,
the IDisposable.Dispose() method is called by the form from inside to
release the resource explicitly.

As for the derived Form class, what we need to do is to override the
Dispose(bool) method should to contain extra code to release resources the
derived form class is holding, if there's any.

You could put your code for releasing the managed/native resources in the
existing override Dispose(boo) method in the design file.

Hope I make some clarification.

If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Stoitcho Goutsev \(100\)

Max2006,

This restriction of not modifying a method goes only for
InitializeComponent. You can go ahead and add your stuff to the dispose
method. It is moved to the designer file just because this is a method that
usually porgrammers doesn't modify. You need to dispose only unmanaged
resource managed resources are taken care of the GC. Using unmanaged
resources is not a common thing for .NET applications.
 
M

Max2006

Hi Linda,



Thank you for help.



I understand that you suggest that we modify the Dispose method within
Form.Designer.cs file.



Please correct me if I am wrong. I like what you suggest; however, at this
point, we have a debate among our project team about whether we should
modify the Designer file or not.



Would you be able to send me a link to MSDN or an online article that
supports modifying Dispose method in Designer file?



Thank you again,

Max
 
L

Linda Liu [MSFT]

Hi Max,

Thank you for your prompt response.

Yes, you understand my suggestion very well.

I have tried to search the Internet for an MSDN or KB article that supports
modifying Dispose method in the Designer file, but unfortunately, I have
not found one. All documents say we should not modify the form.Designer.cs
file manually, but in fact they all only mean we should not modify the
InitializeComponent method in the form.Designer.cs file.

In the form.Designer.cs file, you could see that the InitializeCompoent
method is embraced in a region named 'Windows Form Designer generated
code', which means that Windows Forms Designer only takes charge of the
InitializeComponent method, not including the Dispose method.

I think the reason why the Dispose method is placed in the form.Designer.cs
file is that generally we needn't modifiy the Dispose method at all.

Alternatively, I think you could put your code for releasing the resources
the form is holding in the FormClosed event handler.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Max,

How about the problem now?

If you have any concerns, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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