By   January 23, 2014

    public class MyClass : IDisposable
    {
        private readonly Timer _timer;

        public MyClass()
        {
            _timer = new Timer(Timer_OnElapsed, null, 0, 1000);
        }

        private void Timer_OnElapsed(object sender)
        {
        }

        #region IDisposable members

        private bool _disposed = false;

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

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _timer.Dispose();
                }
                _disposed = true;
            }
        }

        ~MyClass()
        {
            Dispose(false);
        }

        #endregion IDisposable members
    }

Leave a Reply

Your email address will not be published. Required fields are marked *