[Mono-list] Dispose method is never called

Giuseppe Greco giuseppe.greco@agamura.com
06 Jul 2003 17:20:14 +0200


Hi all,

I've a class that starts a worker thread like
this:

class MyClass
{
  bool isDisposed = false;
  Thread myThread;

  public MyClass()
  {
      ...
      myThread = new Thread(new ThreadStart(MyThreadMethod));
      myThread.Start();
      myThread.IsBackground = true;
   }

  ~MyClass()
  {
    Dispose(false)
  }

  public void Dispose()
  {
    Dispose(true);
    GC.SuppressFinalize(this);
  }

  protected virtual void Dispose(bool disposing)
  {
    if (!isDisposed) {
      isDisposed = true;

      myThread.Join(); // should wait until MyThreadMethod()
                       // completes its work...
      myThread = null;
                                                                 
      if (disposing) {
        ...
      }
    }
  }

  private void MyThreadMethod()
  {
    while (!isDisposed) {
      ...
    {
  }
}

Well, in the class above, the Dispose() method is never
called. This is a problem if one needs to wait until the
thread has finished its work -- Thread.Join() should block
until then.

The destructor -- ~MyClass() -- is never called.

Gius_.

-- 
----------------------------------------
Giuseppe Greco

::agamura::

phone:  +41 (0)91 604 67 65
mobile: +41 (0)76 390 60 32
email:  giuseppe.greco@agamura.com
web:    www.agamura.com
----------------------------------------