George George

George George

  • NA
  • 778
  • 0

thread safe in Dispose method

May 14 2008 8:58 AM

Hello everyone,


Here is my code for Dispose method. Currently, I am thinking whether it is necessary to make it thread safe by adding a lock to prevent two threads to execute Dispose methods at the same time?

Notes:

1. disposing is used to indicate explicit call to Dispose or implicit call from Finalizer;

2. disposed is used to indicate whether current instance is Disposed or not, to prevent from disposing again. The thread safety consideration is used to protect this variable.

[Code]
        protected virtual void Dispose(bool disposing)
        {
            lock (DisposeLock)
            {
                if ((false == disposed) && (true == disposing))
                {
   // some operations here
                    disposed = true;
                }
            }
        }
[/Code]


thanks in advance,
George


Answers (2)