I want to be able to do something else while the class enters GC - no more
references or the runner exites [without doing anything in runner code] (as
it was
~Foo()
{
StopDoingSomething();
}
does not works.
Please advice
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
You can execute code in the finalizer. However, that code doesn't run
when there are no more references to it. Rather, it runs when the object is
garbage collected. If you have code that depends on being called when the
object is disposed of, then you should implement the IDisposable interface.
Check out the section of the .NET framework general reference titled
"Implementing Finalize and Dispose to Clean Up Unmanaged Resources", located
at (watch for line wrap):
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"Tamir Khason" <tamir-...@tcon-NOSPAM.co.il> wrote in message
news:uNwSUGL...@TK2MSFTNGP14.phx.gbl...
StartDoingThis()
{
Console.Write("Stop");
}
StartDoingThis()
{
Console.Write("Start");
}
}
public class Caller
{
public Caller()
{
Foo f = new Foo();
}
}
While running Caller it appears:
"Start" and the program exits. How to make it writing "Stop". What I'm doing
wrong?
Thank you
"Nicholas Paldino [.NET/C# MVP]" <m...@spam.guard.caspershouse.com> wrote in
message news:efAMSNLp...@TK2MSFTNGP14.phx.gbl...
If you're using the Dispose pattern, you need to class Dispose explicitly:
Foo f = new Foo(); // this will output "Start"
f.Dispose(); // this will output "Stop"
If you don't call Dispose, then the finalizer (~Foo()) will call Dispose(false), and not execute StopDoingThis(), so "Stop" will not be outputted.
For more information on Dispose, check out these two blog entries:
http://blogs.msdn.com/clyon/archive/2004/09/21/232445.aspx
http://blogs.msdn.com/clyon/archive/2004/09/23/233464.aspx
Hope that helps
-Chris
--------------------
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
StopDoingThis() is only invoked if Dispose() is invoked.
i.e.
Foo f = new Foo();
f.Dispose();
or
using(Foo f = new Foo())
{
}
will both do the trick.
--
Scott
http://www.OdeToCode.com
""Chris Lyon [MSFT]"" <cl...@online.microsoft.com> wrote in message
news:aNL7pjLp...@cpmsftngxa06.phx.gbl...
> I do not want explicitly call f.Dispose
> in my Finalizer i', using Dispose(false) but nothing happens
> see in code
> ~Foo()
> {
> Dispose(false);
> }
You have to call it. C# (and the .NET CRL in general) doesn't have
a deterministic destructor semantic. Either you use the IDisposable
pattern (see link), or you are on your own.
bye
Rob
public class Caller
{
public Caller()
{
Foo f = new Foo();
GC.Collect();
GC.WaitForPendingFinalizers()l
}
}
I'm not advising you to write production code like this, it just demonstrates that Finalization happens. The key thing about Finalization is it will only happen when the GC realises the object has no live references to it anymore. However, the GC doesn't run the finalizer, it queues the finalizer to be run on a background thread, the finalization thread. The GC will only run when a resource threshhold is hit. IDisposable is a much better way to make sure that clean up code is run at a determined point in time. There is one problem, the runtime knows absolutely nothing about IDisposable and so will not run the Dispose method for you.The consumers of your object will need to call Dispose on it. C# has an exception proof syntax for calling Dispose - the using statement.
So for your second code example (IDisposable one) write the Caller as follows:
public class Caller
{
public Caller()
{
using(Foo f = new Foo())
{
// your code that uses f goes here
}
}
}
However, may not be the usage scenario you want. IDisposable doesn't have a happy relationship with the concept of "lack of ownership" it really depends on one piece of code having ownership of the object and it determining when to call Dispose.
Regards
Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<uhWbubLp...@TK2MSFTNGP09.phx.gbl>
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 24/09/2004
[microsoft.public.dotnet.languages.csharp]