comparison 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
comparison
equal deleted inserted replaced
249:d82909310094 250:9f63dade3a40
7 /// <summary> 7 /// <summary>
8 /// Base class the objects which support disposing. 8 /// Base class the objects which support disposing.
9 /// </summary> 9 /// </summary>
10 public class Disposable : IDisposable { 10 public class Disposable : IDisposable {
11 11
12 int m_disposed;
13
14 public event EventHandler Disposed; 12 public event EventHandler Disposed;
15 13
16 public bool IsDisposed { 14 public bool IsDisposed {
17 get { 15 get; private set;
18 Thread.MemoryBarrier();
19 return m_disposed != 0;
20 }
21 } 16 }
22 17
23 /// <summary> 18 /// <summary>
24 /// Asserts the object is not disposed. 19 /// Asserts the object is not disposed.
25 /// </summary> 20 /// </summary>
26 /// <exception cref="ObjectDisposedException">The object is disposed</exception> 21 /// <exception cref="ObjectDisposedException">The object is disposed</exception>
27 /// <remarks> 22 /// <remarks>
28 /// Успешная проверка того, что объект не освобожден еще не гарантирует, что он не
29 /// будет освобожден сразу после нее, поэтому методы использующие проверку должны
30 /// учитывать, что объект может быть освобожден из параллельного потока.
31 /// Данны метод служит для упрощения отладки ошибок при использовании объекта после его
32 /// освобождения.
33 /// </remarks>
34 /// <example>
35 /// // пример синхронизированного освобождения ресурсов
36 /// class FileStore : Disposable {
37 /// readonly TextWriter m_file;
38 /// readonly obejct m_sync = new object();
39 ///
40 /// public FileStore(string file) {
41 /// m_file = new TextWriter(File.OpenWrite(file));
42 /// }
43 ///
44 /// public void Write(string text) {
45 /// lock(m_sync) {
46 /// AssertNotDisposed();
47 /// m_file.Write(text);
48 /// }
49 /// }
50 ///
51 /// protected override void Dispose(bool disposing) {
52 /// if (disposing)
53 /// lock(m_sync) {
54 /// m_file.Dipose();
55 /// base.Dispose(true);
56 /// }
57 /// else
58 /// base.Dispose(false);
59 /// }
60 /// }
61 /// <example>
62 protected void AssertNotDisposed() { 23 protected void AssertNotDisposed() {
63 Thread.MemoryBarrier(); 24 if (IsDisposed)
64 if (m_disposed != 0)
65 throw new ObjectDisposedException(ToString()); 25 throw new ObjectDisposedException(ToString());
66 } 26 }
67 /// <summary> 27 /// <summary>
68 /// Вызывает событие <see cref="Disposed"/> 28 /// Вызывает событие <see cref="Disposed"/>
69 /// </summary> 29 /// </summary>
75 /// из нескольких потоков. 35 /// из нескольких потоков.
76 /// </remarks> 36 /// </remarks>
77 protected virtual void Dispose(bool disposing) { 37 protected virtual void Dispose(bool disposing) {
78 if (disposing) 38 if (disposing)
79 Disposed.DispatchEvent(this, EventArgs.Empty); 39 Disposed.DispatchEvent(this, EventArgs.Empty);
80
81 } 40 }
82 41
83 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")] 42 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
84 public void Dispose() { 43 public void Dispose() {
85 if (Interlocked.Increment(ref m_disposed) == 1) { 44 if(!IsDisposed) {
45 IsDisposed = true;
86 Dispose(true); 46 Dispose(true);
87 GC.SuppressFinalize(this); 47 GC.SuppressFinalize(this);
88 } 48 }
89 } 49 }
90 50