473,386 Members | 1,738 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,386 software developers and data experts.

Dispose for UserControl

Hi,

I have a UserControl (C# WinFroms application) that requires some cleaning
code to run when a form with control is closed. Where I can put that code?

There is no ControlClosing or ControlClosed event.
I can put my code into UserControl.Desgner.cs Dispose method, but that seems
to be a bad practive.

What do I do?

Thanks.

Jun 27 '08 #1
6 23315
Don't modify the designer files if you can help it. If the file changes a
lot of times the designer will overwrite any work you've put into the files
if it needs to change that piece. That's why the designer files say to not
modify them and why partial classes are so good. :o)

In your control, override the Dispose(bool) method and handle your cleanup
there. You'll want to make sure you only handle the disposal when disposing
is true, false gets passed to the method when the destructor gets called by
the garbage collector. In which case if it is false, the objects you're
trying to clean up have already been released and will be null. Then, in
your form's closing event, simply call the Dispose() method on your user
control.

Also, about disposal of private objects - generally speaking, I tend to only
implement IDisposable when the private fields of a type implement
IDisposable themselves.

"Victor" <Vi****@discussions.microsoft.comwrote in message
news:D3**********************************@microsof t.com...
Hi,

I have a UserControl (C# WinFroms application) that requires some cleaning
code to run when a form with control is closed. Where I can put that
code?

There is no ControlClosing or ControlClosed event.
I can put my code into UserControl.Desgner.cs Dispose method, but that
seems
to be a bad practive.

What do I do?

Thanks.
Jun 27 '08 #2
Jeff,

thanks for you answer.

I can not override Dispose(bool disposing) in my code, since it has been
already 'overridden' in UserControl.Designer.cs autogenerated file.

Any attempt to override it again in UserControl.cs results in a compilation
error.

Victor

"Jeff Winn" wrote:
Don't modify the designer files if you can help it. If the file changes a
lot of times the designer will overwrite any work you've put into the files
if it needs to change that piece. That's why the designer files say to not
modify them and why partial classes are so good. :o)

In your control, override the Dispose(bool) method and handle your cleanup
there. You'll want to make sure you only handle the disposal when disposing
is true, false gets passed to the method when the destructor gets called by
the garbage collector. In which case if it is false, the objects you're
trying to clean up have already been released and will be null. Then, in
your form's closing event, simply call the Dispose() method on your user
control.

Also, about disposal of private objects - generally speaking, I tend to only
implement IDisposable when the private fields of a type implement
IDisposable themselves.

"Victor" <Vi****@discussions.microsoft.comwrote in message
news:D3**********************************@microsof t.com...
Hi,

I have a UserControl (C# WinFroms application) that requires some cleaning
code to run when a form with control is closed. Where I can put that
code?

There is no ControlClosing or ControlClosed event.
I can put my code into UserControl.Desgner.cs Dispose method, but that
seems
to be a bad practive.

What do I do?

Thanks.

Jun 27 '08 #3
Oops, I didn't realize the designer overrides the method. Makes sense though
considering it's a container for other controls. Perhaps rethinking the
design would be useful so you aren't having to dispose of the object from
the control. However, that method should be fine if you moved it out of the
designer and into the code file for the control.

"Victor" <Vi****@discussions.microsoft.comwrote in message
news:AA**********************************@microsof t.com...
Jeff,

thanks for you answer.

I can not override Dispose(bool disposing) in my code, since it has been
already 'overridden' in UserControl.Designer.cs autogenerated file.

Any attempt to override it again in UserControl.cs results in a
compilation
error.

Victor

"Jeff Winn" wrote:
>Don't modify the designer files if you can help it. If the file changes a
lot of times the designer will overwrite any work you've put into the
files
if it needs to change that piece. That's why the designer files say to
not
modify them and why partial classes are so good. :o)

In your control, override the Dispose(bool) method and handle your
cleanup
there. You'll want to make sure you only handle the disposal when
disposing
is true, false gets passed to the method when the destructor gets called
by
the garbage collector. In which case if it is false, the objects you're
trying to clean up have already been released and will be null. Then, in
your form's closing event, simply call the Dispose() method on your user
control.

Also, about disposal of private objects - generally speaking, I tend to
only
implement IDisposable when the private fields of a type implement
IDisposable themselves.

"Victor" <Vi****@discussions.microsoft.comwrote in message
news:D3**********************************@microso ft.com...
Hi,

I have a UserControl (C# WinFroms application) that requires some
cleaning
code to run when a form with control is closed. Where I can put that
code?

There is no ControlClosing or ControlClosed event.
I can put my code into UserControl.Desgner.cs Dispose method, but that
seems
to be a bad practive.

What do I do?

Thanks.
Jun 27 '08 #4
Thanks Jeff,

Moving the method out of the designer file solves my problem.

Victor

"Jeff Winn" wrote:
Oops, I didn't realize the designer overrides the method. Makes sense though
considering it's a container for other controls. Perhaps rethinking the
design would be useful so you aren't having to dispose of the object from
the control. However, that method should be fine if you moved it out of the
designer and into the code file for the control.

"Victor" <Vi****@discussions.microsoft.comwrote in message
news:AA**********************************@microsof t.com...
Jeff,

thanks for you answer.

I can not override Dispose(bool disposing) in my code, since it has been
already 'overridden' in UserControl.Designer.cs autogenerated file.

Any attempt to override it again in UserControl.cs results in a
compilation
error.

Victor

"Jeff Winn" wrote:
Don't modify the designer files if you can help it. If the file changes a
lot of times the designer will overwrite any work you've put into the
files
if it needs to change that piece. That's why the designer files say to
not
modify them and why partial classes are so good. :o)

In your control, override the Dispose(bool) method and handle your
cleanup
there. You'll want to make sure you only handle the disposal when
disposing
is true, false gets passed to the method when the destructor gets called
by
the garbage collector. In which case if it is false, the objects you're
trying to clean up have already been released and will be null. Then, in
your form's closing event, simply call the Dispose() method on your user
control.

Also, about disposal of private objects - generally speaking, I tend to
only
implement IDisposable when the private fields of a type implement
IDisposable themselves.

"Victor" <Vi****@discussions.microsoft.comwrote in message
news:D3**********************************@microsof t.com...
Hi,

I have a UserControl (C# WinFroms application) that requires some
cleaning
code to run when a form with control is closed. Where I can put that
code?

There is no ControlClosing or ControlClosed event.
I can put my code into UserControl.Desgner.cs Dispose method, but that
seems
to be a bad practive.

What do I do?

Thanks.

Jun 27 '08 #5
In your control, override the Dispose(bool) method and handle your cleanup
there. You'll want to make sure you only handle the disposal when disposing
is true, false gets passed to the method when the destructor gets called by
the garbage collector. In which case if it is false, the objects you're
trying to clean up have already been released and will be null.
Hi Jeff.

Are you sure about this? Its my understanding that even if the
finalizer is being called by the garbage collector you are still able
to get a handle to the other child objects.

Of course, referencing a child object while the garbage collector is
disposing of them may be a bad idea because the object that you are
calling may have had their Dispose method already called but that
should be another story.

I put together a little sample to test this, if you run the sample
below, even if “ChildClass” get dispossed first, “ParentClass” is
still able to successfully get a reference to the “ChildClass”.

namespace ConsoleApplication393
{
class Program
{
static void Main(string[] args)
{
ParentClass m = new ParentClass();
}
}

public class ParentClass
{
ChildClass m_Caca = new ChildClass();

public ParentClass()
{

}

~ParentClass()
{
// No problems here even if "ChildClass" has been
disposed.
int o = m_Caca.SomeInt;
}
}

public class ChildClass
{
public int SomeInt = 123;

public ChildClass()
{
}

~ChildClass()
{
SomeInt = 999;
}
}
}

Is that what you meant?

Thanks.
Jun 27 '08 #6
It depends on how the class you're using implemented the IDisposable
interface. Below is an example of how I typically implement the interface.
The problem when relying on the destructor to clean up your object when
using child objects that implement the IDisposable interface - they may have
already been disposed of by the time the destructor on your class gets
called. Which is why whenever an object implements IDisposable you're
supposed to call Dispose on your own when you're done with it to ensure
everything within the object gets cleaned up properly.

As for your example below, we were talking about the use of IDisposable -
not whether you can access child objects from an objects destructor.
Implement IDisposable in both of your classes, call dispose on the parent
object and then let the destructor try and access it. It should throw an
exception about trying to use an object that's already been disposed of.

static class Program {
static void Main(string[] args) {
using (DisposableObject o = new DisposableObject()) {
o.DoWork();
}
}
}

class DisposableObject : IDisposable {
Stream _s;

public DisposableObject() {
this._s = new MemoryStream();
}

~DisposableObject() {
this.Dispose(false);
}

public void Dispose() {
this.Dispose(true);
GC.SupressFinalize(this);
}

public void DoWork() {
}

void Dispose(bool disposing) {
if (disposing) {
this._s.Dispose();
}
}
}

<qg**********@mailinator.comwrote in message
news:ff**********************************@2g2000hs n.googlegroups.com...
In your control, override the Dispose(bool) method and handle your cleanup
there. You'll want to make sure you only handle the disposal when
disposing
is true, false gets passed to the method when the destructor gets called
by
the garbage collector. In which case if it is false, the objects you're
trying to clean up have already been released and will be null.
Hi Jeff.

Are you sure about this? Its my understanding that even if the
finalizer is being called by the garbage collector you are still able
to get a handle to the other child objects.

Of course, referencing a child object while the garbage collector is
disposing of them may be a bad idea because the object that you are
calling may have had their Dispose method already called but that
should be another story.

I put together a little sample to test this, if you run the sample
below, even if “ChildClass” get dispossed first, “ParentClass” is
still able to successfully get a reference to the “ChildClass”.

namespace ConsoleApplication393
{
class Program
{
static void Main(string[] args)
{
ParentClass m = new ParentClass();
}
}

public class ParentClass
{
ChildClass m_Caca = new ChildClass();

public ParentClass()
{

}

~ParentClass()
{
// No problems here even if "ChildClass" has been
disposed.
int o = m_Caca.SomeInt;
}
}

public class ChildClass
{
public int SomeInt = 123;

public ChildClass()
{
}

~ChildClass()
{
SomeInt = 999;
}
}
}

Is that what you meant?

Thanks.

Jun 27 '08 #7

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

Similar topics

1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
2
by: Jaikumar | last post by:
Hi, 1) I have created one windows application, In the main form ( form1) i have added one usercontrol (usercontrol1), In that user control i am drawing one image. 2) In the UserControl1 i am...
1
by: MuZZy | last post by:
HI, I have a user control, say with one textBox. I need the following: when the user closes form (either calling Form.Close, or pressing "X" or anyhow), UserControl copies a file from a...
9
by: Charles Law | last post by:
I have a form on which user controls are placed at runtime. When a control is added to the form a handler is added for an event that a high-level object raises, which must be handled by the new...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
7
by: Peter Proost | last post by:
Hi group, Here at work they've got an activeX control which is used on a usercontrol, and this usercontrol is one of the standard controls in the framework, so this user controls get's used a...
2
by: Steve | last post by:
This is a weird one. I have a series of "SmartParts" which are CAB (Composite Application Block) Views which are finally just UserControls (99% of the time) Anyway, I layout my UserControl in...
4
by: Martin | last post by:
Hi everyone ! For those who haven't read my previous post, I have a problem with a UserControl. When I try to display it on a form, controls seem to appear one after one. In the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.