Removing methods calls off the stack in C#
how can remove method calls off stack of object in c#?
i set variable equal null object still executing code.
for example if have onc class calling method of other , calling other methods in class this:
class a { private b var; void start() { var = new b; var.runflag true; var.run(); } void stop() { var.runflag = false; var.dispse(); var = null; } } class b : idisposable { public bool runflag = false; void run() { while(runflag) { meth1(); meth2(); meth3(); } } void meth1() { dosomething(); } void meth2() { dosomething(); } void meth3() { dosomething(); } void dosomething() { return; } void dispose() { // clean resources } } |
then after call in stop() method of class a, b should garabage collected no matter control of execution is. right or wrong.
please help!!!!!!!!!!!!!!!!!!!!!!!!
i think i need explain you. removing object off stack can done, not "forcing" gc pull off, "asking" it. now, if gc determines right time pull object off, biding, if has not, won't it. gc knowing, omni-present, greater-being. can , ask, cannot command.
example:
using system;
namespace unmanagedresourceobjlib ~ownsunmanagedresources() void cleanup() public void close() #region idisposable members bool _disposed = false; #endregion public void someotheroperation() using system; namespace funwithgc // obj.dispose(); static void main()
{
public class ownsunmanagedresources : idisposable
{
public ownsunmanagedresources()
{
console.writeline("ownsunmanagedresources obj constructed");
}
{
console.writeline("ownsunmanagedresources obj de-structed (finalize called)");
cleanup();
}
{
console.writeline("ownsunmanagedresources obj cleanup occurred");
}
{
this.dispose();
}
public void dispose()
{
if(this._disposed)
return;
else
this._disposed = true;
gc.suppressfinalize(this);
console.writeline("ownsunmanagedresources obj disposed");
cleanup();
}
{
if(this._disposed)
throw new objectdisposedexception("ownsunmanagedresources");
console.writeline("ownsunmanagedresources obj: someotheroperation");
}
}
}
{
class app
{
static void usesobj()
{
#if evil
unmanagedresourceobjlib.ownsunmanagedresources obj = new unmanagedresourceobjlib.ownsunmanagedresources();
// obj.dispose();
// obj out of scope
obj.someotheroperation();
using(obj)
{
obj.someotheroperation();
}
obj.someotheroperation();
#else
using(unmanagedresourceobjlib.ownsunmanagedresources obj = new unmanagedresourceobjlib.ownsunmanagedresources())
{
using(unmanagedresourceobjlib.ownsunmanagedresources obj2 = new unmanagedresourceobjlib.ownsunmanagedresources())
obj2.someotheroperation();
obj.someotheroperation();
}
#endif
}
{
console.writeline("main started");
console.writeline("usesobj method called");
usesobj();
console.writeline("usesobj method returned");
console.writeline("main terminating");
}
}
}
reference unmanagedresourceobject funwithgc. understand how gc destroys object , takes off stack.
Archived Forums V > Visual C# Language
Comments
Post a Comment