Home All Groups Group Topic Archive Search About

How to know when a Class/Object is Disposed/Finalized/ = Nothing

Author
14 Oct 2005 10:18 AM
DraguVaso
Hi,

I have made some classes, but when they are Finalized/Disposed or simply
"MyClass = Nothing", I want to trigger this action, and ask to save unsaved
changes.

How should I do this? I'm kind of testing with a Base-Class from which all
my classes inherit to override the Finalize-method, but it doesn't really
work... :-(

Any help our hints would be really appreciated!

Thanks a lot in advance,

Pieter

Author
14 Oct 2005 10:52 AM
Ken Tucker [MVP]
Hi,

        Try having your class implement Idisposable that will add a dispose
method where you can clean things up.  I dont think setting the class to
nothing will trigger dispose.  Try using the myclass.dispose instead of
myclass=nothing

Public Class test

Implements IDisposable

Public Sub Dispose() Implements System.IDisposable.Dispose

MessageBox.Show("Disposing")

End Sub

End Class



Ken

-----------------------

Show quote
"DraguVaso" <pietercou***@hotmail.com> wrote in message
news:%23vn5PiK0FHA.1028@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I have made some classes, but when they are Finalized/Disposed or simply
> "MyClass = Nothing", I want to trigger this action, and ask to save
> unsaved changes.
>
> How should I do this? I'm kind of testing with a Base-Class from which all
> my classes inherit to override the Finalize-method, but it doesn't really
> work... :-(
>
> Any help our hints would be really appreciated!
>
> Thanks a lot in advance,
>
> Pieter
>
>
>
Author
14 Oct 2005 11:10 AM
Kevin Spencer
Hi Pieter,

Check out the following SDK reference on the IDisposable Interface. It
includes (int the Remarks section) good advice and an excellent sample of
the way you ought to implement the Dispose method while avoiding the types
of problems you're describing.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemidisposableclasstopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

Show quote
"DraguVaso" <pietercou***@hotmail.com> wrote in message
news:%23vn5PiK0FHA.1028@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I have made some classes, but when they are Finalized/Disposed or simply
> "MyClass = Nothing", I want to trigger this action, and ask to save
> unsaved changes.
>
> How should I do this? I'm kind of testing with a Base-Class from which all
> my classes inherit to override the Finalize-method, but it doesn't really
> work... :-(
>
> Any help our hints would be really appreciated!
>
> Thanks a lot in advance,
>
> Pieter
>
>
>
Author
14 Oct 2005 11:32 AM
Phill. W
"DraguVaso" <pietercou***@hotmail.com> wrote in message
news:%23vn5PiK0FHA.1028@TK2MSFTNGP12.phx.gbl...

> I have made some classes, but when they are Finalized/Disposed
> or simply "MyClass = Nothing", I want to trigger this action, and
> ask to save unsaved changes.

Finalizing/Disposing are definitely not the same as setting a
Variable to Nothing.

If you have code that you want to be called when the object is
"no longer needed", have the class implement IDisposable and
put the code into your Dispose method (have a look at the
Dispose code written into any Windows Forms you create).

It is /convention/ that when discarding an object that /has/ a
Dispose method, the caller /should/ do so (sadly, this isn't
actually /enforced/ anywhere).

Also, with regards this bit ...

"ask to save unsaved changes"

.... spare a thought for /how/ you intend to do this.
The quick answer is to pop up a MessageBox - but this could
leave you a bit stuck further down the line if you want to, say,
reuse these classes in a Windows Service; these don't like
MessageBox's very much at all.

HTH,
    Phill  W.
Author
14 Oct 2005 8:40 PM
branco.medeiros
DraguVaso wrote:
<snip>
> I have made some classes, but when they are Finalized/Disposed or simply
> "MyClass = Nothing", I want to trigger this action, and ask to save unsaved
> changes.
>
> How should I do this? I'm kind of testing with a Base-Class from which all
> my classes inherit to override the Finalize-method, but it doesn't really
> work... :-(
<snip>

Alas, you're asking for deterministic finalization, and this is not
provided in .Net. The closest to this is an Open/Close pattern: you
implement these methods and the clients (users of your class) must call
them at the appropriate times.

This way you may have some tracking logic inside your class (reference
counting, for exemple), that would detect the last 'Close' and take
action if the data wasn't saved (raise an event, for example).

Hope this helps...

Regards,

Branco.
Author
17 Oct 2005 7:17 AM
DraguVaso
ok, thanks a lot for the help guys!

Pieter

<branco.medei***@gmail.com> wrote in message
Show quote
news:1129322404.851853.54470@g44g2000cwa.googlegroups.com...
>
> DraguVaso wrote:
> <snip>
>> I have made some classes, but when they are Finalized/Disposed or simply
>> "MyClass = Nothing", I want to trigger this action, and ask to save
>> unsaved
>> changes.
>>
>> How should I do this? I'm kind of testing with a Base-Class from which
>> all
>> my classes inherit to override the Finalize-method, but it doesn't really
>> work... :-(
> <snip>
>
> Alas, you're asking for deterministic finalization, and this is not
> provided in .Net. The closest to this is an Open/Close pattern: you
> implement these methods and the clients (users of your class) must call
> them at the appropriate times.
>
> This way you may have some tracking logic inside your class (reference
> counting, for exemple), that would detect the last 'Close' and take
> action if the data wasn't saved (raise an event, for example).
>
> Hope this helps...
>
> Regards,
>
> Branco.
>

AddThis Social Bookmark Button