C# how to implement Dispose method

前端 未结 3 645
温柔的废话
温柔的废话 2021-01-30 13:51

I need some advice on the implementation of the Dispose method.

In our application the user designs their own UI. I have a preview window that shows what t

3条回答
  •  攒了一身酷
    2021-01-30 14:14

    I Implement IDisposable

     class ConnectionConfiguration:IDisposable
    {
        private static volatile IConnection _rbMqconnection;
        private static readonly object ConnectionLock = new object();
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }
            if (_rbMqconnection == null)
            {
                return;
            }
            lock (ConnectionLock)
            {
                if (_rbMqconnection == null)
                {
                    return;
                }
                _rbMqconnection?.Dispose();//double check
                _rbMqconnection = null;
            }
        }
    }
    

提交回复
热议问题