PC Review


Reply
Thread Tools Rate Thread

Dispose for UserControl

 
 
Victor
Guest
Posts: n/a
 
      23rd Jun 2008
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.



 
Reply With Quote
 
 
 
 
Jeff Winn
Guest
Posts: n/a
 
      24th Jun 2008
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. )

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" <(E-Mail Removed)> wrote in message
news385E087-9C13-4BF0-9E31-(E-Mail Removed)...
> 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.
>
>
>


 
Reply With Quote
 
Victor
Guest
Posts: n/a
 
      24th Jun 2008
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. )
>
> 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" <(E-Mail Removed)> wrote in message
> news385E087-9C13-4BF0-9E31-(E-Mail Removed)...
> > 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.
> >
> >
> >

>

 
Reply With Quote
 
Jeff Winn
Guest
Posts: n/a
 
      24th Jun 2008
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" <(E-Mail Removed)> wrote in message
news:AA260A09-617B-44C3-BE66-(E-Mail Removed)...
> 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. )
>>
>> 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" <(E-Mail Removed)> wrote in message
>> news385E087-9C13-4BF0-9E31-(E-Mail Removed)...
>> > 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.
>> >
>> >
>> >

>>


 
Reply With Quote
 
Victor
Guest
Posts: n/a
 
      24th Jun 2008
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" <(E-Mail Removed)> wrote in message
> news:AA260A09-617B-44C3-BE66-(E-Mail Removed)...
> > 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. )
> >>
> >> 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" <(E-Mail Removed)> wrote in message
> >> news385E087-9C13-4BF0-9E31-(E-Mail Removed)...
> >> > 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.
> >> >
> >> >
> >> >
> >>

>

 
Reply With Quote
 
qglyirnyfgfo@mailinator.com
Guest
Posts: n/a
 
      24th Jun 2008
> 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.
 
Reply With Quote
 
Jeff Winn
Guest
Posts: n/a
 
      24th Jun 2008
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();
}
}
}

<(E-Mail Removed)> wrote in message
news:ff65e39e-51c5-4959-9ef1-(E-Mail Removed)...
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CF2 UserControl Dispose Ryan Microsoft Dot NET Compact Framework 2 12th Oct 2006 12:56 PM
the difference betweenSqlConnection.IDisposable.Dispose() andSqlConnection.Dispose(). tangyong Microsoft Dot NET Framework 1 20th Jan 2006 04:53 AM
VS.NET (2.0), UserControl & Dispose Lloyd Dupont Microsoft Dot NET Framework 2 12th Sep 2005 01:59 PM
Bug report: Dispose for UserControl causes infinite loop =?Utf-8?B?S2VuIEppYW5n?= Microsoft Dot NET Framework Forms 3 29th Oct 2004 04:23 AM
UserControl's and Dispose/GC.Collect question james Microsoft C# .NET 2 21st Nov 2003 04:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:56 PM.