473,386 Members | 1,804 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.

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

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

Oct 14 '05 #1
5 1639
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

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

"DraguVaso" <pi**********@hotmail.com> wrote in message
news:%2****************@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

Oct 14 '05 #2
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/de...classtopic.asp

--
HTH,

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

"DraguVaso" <pi**********@hotmail.com> wrote in message
news:%2****************@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

Oct 14 '05 #3
"DraguVaso" <pi**********@hotmail.com> wrote in message
news:%2****************@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.
Oct 14 '05 #4

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.

Oct 14 '05 #5
ok, thanks a lot for the help guys!

Pieter

<br*************@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.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.

Oct 17 '05 #6

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

Similar topics

21
by: SamSpade | last post by:
I declare a variable X of type myForm then instantiate myForm saving a reference in the variable X, and then do X.Show. If I need to know if the form has been shown yet, I check X against...
6
by: SamSpade | last post by:
Public Function PicCreateGraphics() As Graphics 'Client should dispose this PicCreateGraphics = Graphics.FromImage(mDocumentImage) mPicCreateGraphicsSaved = PicCreateGraphics 'Saved so I can...
3
by: Dean Slindee | last post by:
When launching another form from the current form, I use the code below to determine if the form to be launched is already active and showing. If form = Nothing, then I instantiate it, otherwise I...
5
by: DraguVaso | last post by:
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...
8
by: Diego F. | last post by:
Hello. I'd like to know if there's a way to know if all threads started using the threadpool are finished. How can I ensure that all threads finished? -- Regards, Diego F.
2
by: LB | last post by:
How may i do to know if a form is already open ?? In vb6, i use Forms but i don't know how to do this in vbnet. Thank you!!!
5
by: aine_canby | last post by:
Hi, The problem is that the line - this.Invoke(new MyDelegate(Function), args); // this = MainForm is being called after the line - terminatePopulate = true; therefore I get an exception...
1
by: Joseph Geretz | last post by:
If an HttpHandler is implementing IsReusable = true, is the instance notitifed when it is actually released to the pool? Of course, each instance is released to the pool when ProcessRequest...
9
by: Duggi | last post by:
I used to wonder why MS implemented C# to accept the following code using (Font f = new Font()) { // some code here. } While the same can be achieved through {
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.