Removing methods calls off the stack in C#


idea

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
{
 public class ownsunmanagedresources : idisposable
 {
  public ownsunmanagedresources()
  {
   console.writeline("ownsunmanagedresources obj constructed");
  }

  ~ownsunmanagedresources()
  {
   console.writeline("ownsunmanagedresources obj de-structed (finalize called)");
   cleanup();
  }

  void cleanup()
  {
   console.writeline("ownsunmanagedresources obj cleanup occurred");
  }

  public void close()
  {
   this.dispose();
  }

  #region idisposable members

  bool _disposed = false;
  public void dispose()
  {
   if(this._disposed)
    return;
   else
    this._disposed = true;
   gc.suppressfinalize(this);
   console.writeline("ownsunmanagedresources obj disposed");  
   cleanup(); 
  }

  #endregion

  public void someotheroperation()
  {
   if(this._disposed)
    throw new objectdisposedexception("ownsunmanagedresources");
   console.writeline("ownsunmanagedresources obj: someotheroperation");
  }
 }
}


 





using system;

namespace funwithgc
{
 class app
 {
  static void usesobj()
  {
#if evil
   unmanagedresourceobjlib.ownsunmanagedresources obj = new unmanagedresourceobjlib.ownsunmanagedresources();

//   obj.dispose();
//   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
  }

  static void main()
  {
   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

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'