Re: Class Dispose

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Tamir Khason (tamir-NOSPAM_at_tcon-NOSPAM.co.il)
Date: 09/27/04


Date: Mon, 27 Sep 2004 19:26:34 +0200

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]" <mvp@spam.guard.caspershouse.com> wrote in
message news:efAMSNLpEHA.3252@TK2MSFTNGP14.phx.gbl...
> 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]
> - mvp@spam.guard.caspershouse.com
>
> "Tamir Khason" <tamir-NOSPAM@tcon-NOSPAM.co.il> wrote in message
> news:uNwSUGLpEHA.132@TK2MSFTNGP14.phx.gbl...
>>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 "
>>
>>
>
>



Relevant Pages

  • Re: ByRef/Ref passing in Web Services
    ... I swear I wrote an example last night which didn't, ... > public int xoxo; ... > public class WebService: System.Web.Services.WebService ...
    (microsoft.public.dotnet.framework)
  • Re: The curse of constant fields
    ... run, you need to recompile. ... public class Foo { ... so a dependency tree is needed after all. ...
    (comp.lang.java.programmer)
  • Re: reference not removed revisited
    ... I tried the inherit but I get an error at 'public class ... Frank ... inherit from SqlDataAdapter and override Dispose, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to get the runtime class?
    ... > abstract public class foo { ... > the runtime class is type foo2 ... an object that is of class "foo2" you have some other problem. ... > public class foo2 extends foo { ...
    (comp.lang.java.programmer)
  • Re: public DataValue this[int column] indexing with int ???
    ... Public Class Foo ... Public Class Fooz ... Sub Main ...
    (microsoft.public.dotnet.languages.csharp)