Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class Dispose

10 views
Skip to first unread message

Tamir Khason

unread,
Sep 27, 2004, 12:48:15 PM9/27/04
to
I have a class
public class Foo
{
public Foo()
{
DoSomething()
}
}

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 "


Nicholas Paldino [.NET/C# MVP]

unread,
Sep 27, 2004, 12:58:33 PM9/27/04
to
Tamir,

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):

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

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...

Tamir Khason

unread,
Sep 27, 2004, 1:26:34 PM9/27/04
to
Thjis still does not work:
public class Foo: IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(disposing)
{
StopDoingThis()
}
}
~Foo()
{
Dispose(false);
}
public Foo()
{
StartDoingThis();
}

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...

Chris Lyon [MSFT]

unread,
Sep 27, 2004, 1:39:23 PM9/27/04
to
Hi Tamir

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.

Scott Allen

unread,
Sep 27, 2004, 1:42:43 PM9/27/04
to
Hi Tamir:

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

Tamir Khason

unread,
Sep 27, 2004, 1:45:50 PM9/27/04
to
I do not want explicitly call f.Dispose
in my Finalizer i', using Dispose(false) but nothing happens
see in code
~Foo()
{
Dispose(false);
}


""Chris Lyon [MSFT]"" <cl...@online.microsoft.com> wrote in message
news:aNL7pjLp...@cpmsftngxa06.phx.gbl...

Robert Jordan

unread,
Sep 27, 2004, 1:54:52 PM9/27/04
to
Tamir Khason wrote:

> 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.

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

bye
Rob

Richard Blewett [DevelopMentor]

unread,
Sep 27, 2004, 2:05:39 PM9/27/04
to microsoft.public.dotnet.languages.csharp
OK to show finalization working flip to a release build with your first example but code the client as:

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]

0 new messages