diff Implab/Components/Disposable.cs @ 250:9f63dade3a40 v3

Working on runnable component
author cin
date Thu, 01 Feb 2018 02:43:35 +0300
parents 9ee78a345738
children 6f4630d0bcd9
line wrap: on
line diff
--- a/Implab/Components/Disposable.cs	Wed Jan 31 11:28:38 2018 +0300
+++ b/Implab/Components/Disposable.cs	Thu Feb 01 02:43:35 2018 +0300
@@ -9,15 +9,10 @@
     /// </summary>
     public class Disposable : IDisposable {
 
-        int m_disposed;
-
         public event EventHandler Disposed;
 
         public bool IsDisposed {
-            get {
-                Thread.MemoryBarrier();
-                return m_disposed != 0;
-            }
+            get; private set;
         }
 
         /// <summary>
@@ -25,43 +20,8 @@
         /// </summary>
         /// <exception cref="ObjectDisposedException">The object is disposed</exception>
         /// <remarks>
-        /// Успешная проверка того, что объект не освобожден еще не гарантирует, что он не
-        /// будет освобожден сразу после нее, поэтому методы использующие проверку должны
-        /// учитывать, что объект может быть освобожден из параллельного потока.
-        /// Данны метод служит для упрощения отладки ошибок при использовании объекта после его
-        /// освобождения.
-        /// </remarks>
-        /// <example>
-        /// // пример синхронизированного освобождения ресурсов
-        /// class FileStore : Disposable {
-        ///     readonly TextWriter m_file;
-        ///     readonly obejct m_sync = new object();
-        /// 
-        ///     public FileStore(string file) {
-        ///         m_file = new TextWriter(File.OpenWrite(file));
-        ///     }
-        /// 
-        ///     public void Write(string text) {
-        ///         lock(m_sync) {
-        ///             AssertNotDisposed();
-        ///             m_file.Write(text);
-        ///         }
-        ///     }
-        /// 
-        ///     protected override void Dispose(bool disposing) {
-        ///         if (disposing)
-        ///             lock(m_sync) {
-        ///                 m_file.Dipose();
-        ///                 base.Dispose(true);
-        ///             }
-        ///         else
-        ///             base.Dispose(false);
-        ///     }
-        /// }
-        /// <example> 
         protected void AssertNotDisposed() {
-            Thread.MemoryBarrier();
-            if (m_disposed != 0)
+            if (IsDisposed)
                 throw new ObjectDisposedException(ToString());
         }
         /// <summary>
@@ -77,12 +37,12 @@
         protected virtual void Dispose(bool disposing) {
             if (disposing)
                 Disposed.DispatchEvent(this, EventArgs.Empty);
-
         }
 
         [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
         public void Dispose() {
-            if (Interlocked.Increment(ref m_disposed) == 1) {
+            if(!IsDisposed) {
+                IsDisposed = true;
                 Dispose(true);
                 GC.SuppressFinalize(this);
             }