By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,171 Members | 1,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,171 IT Pros & Developers. It's quick & easy.

Dispose for UserControl

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.