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 " 7 4447
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/de...izeDispose.asp
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message
news:uN*************@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 "
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ef**************@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/de...izeDispose.asp
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message news:uN*************@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 "
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/...21/232445.aspx http://blogs.msdn.com/clyon/archive/...23/233464.aspx
Hope that helps
-Chris
-------------------- 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]" <mv*@spam.guard.caspershouse.com> wrote in message news:ef**************@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/de...izeDispose.asp
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message news:uN*************@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 "
--
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.
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
On Mon, 27 Sep 2004 19:26:34 +0200, "Tamir Khason"
<ta**********@tcon-NOSPAM.co.il> wrote: 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]" <mv*@spam.guard.caspershouse.com> wrote in message news:ef**************@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/de...izeDispose.asp
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message news:uN*************@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 "
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:aN**************@cpmsftngxa06.phx.gbl... 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/...21/232445.aspx http://blogs.msdn.com/clyon/archive/...23/233464.aspx
Hope that helps -Chris
--------------------
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:ef**************@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/de...izeDispose.asp
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message news:uN*************@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 "
--
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.
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/de...izeDispose.asp
bye
Rob
""Chris Lyon [MSFT]"" <cl***@online.microsoft.com> wrote in message news:aN**************@cpmsftngxa06.phx.gbl...
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/...21/232445.aspx http://blogs.msdn.com/clyon/archive/...23/233464.aspx
Hope that helps -Chris
--------------------
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]" <mv*@spam.guard.caspershouse.com> wrote in message news:ef**************@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/de...izeDispose.asp
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message news:uN*************@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 " > >
--
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.
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/<uh**************@TK2MSFTNGP09.phx.gbl>
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]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ef**************@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/de...izeDispose.asp
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com
"Tamir Khason" <ta**********@tcon-NOSPAM.co.il> wrote in message news:uN*************@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 "
---
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chuck Ritzke |
last post by:
I keep asking myself this question as I write class modules. What's the
best/smartest/most efficient way to send a large object back and forth to a
class module?
For example, say I have a data...
|
by: faktujaa |
last post by:
Hi All,
A small confusion. I have defined a connection class that has
System.Data.IDbConnection as a member variable and implements IDisposable
interface. I have implemented Dispose method to call...
|
by: RiteshDotNet |
last post by:
..net Frame work
1. Dispose Method what it does ?
A. who its call / when it calls ?
B. Is it fire automatically ?
c. When dispose method is call what it does ?
D. Release a Object from memory or...
|
by: Ken Durden |
last post by:
I am in search of a comprehensive methodology of using these two
object cleanup approaches to get rid of a number of bugs,
unpleasantries, and cleanup-ordering issues we currently have in our...
|
by: Fernando Cacciola |
last post by:
Hi People,
Consider the following:
class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }
public void Dispose()
{
|
by: Billy |
last post by:
Hello...
I'm trying to make a database access class for an asp.net
application. When I run my application, the Garbage
Collecter doesn't seems to unload the memory attributed
to my...
|
by: Charles Law |
last post by:
I have a form on which user controls are placed at runtime. When a control
is added to the form a handler is added for an event that a high-level
object raises, which must be handled by the new...
|
by: Dennis |
last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose
method based on advice from this newsgroup. However, I'm not sure how to
dispose of the following object that was created...
|
by: Grafix |
last post by:
All -
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().
In 1.1, we used to have following structure for Dispose
class MyClass : IDisposable
{...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |