annotate Implab/Components/Disposable.cs @ 226:9428ea36838e v2

fixed JSON parser error when parsing escaped double quote symbol
author cin
date Fri, 25 Aug 2017 02:16:35 +0300
parents 9ee78a345738
children 9f63dade3a40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
1 using Implab.Diagnostics;
240aa6994018 component model refactoring
cin
parents:
diff changeset
2 using System;
208
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 152
diff changeset
3 using System.Diagnostics.CodeAnalysis;
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
4 using System.Threading;
240aa6994018 component model refactoring
cin
parents:
diff changeset
5
240aa6994018 component model refactoring
cin
parents:
diff changeset
6 namespace Implab.Components {
240aa6994018 component model refactoring
cin
parents:
diff changeset
7 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
8 /// Base class the objects which support disposing.
240aa6994018 component model refactoring
cin
parents:
diff changeset
9 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
10 public class Disposable : IDisposable {
213
9ee78a345738 Minor code changes
cin
parents: 208
diff changeset
11
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
12 int m_disposed;
240aa6994018 component model refactoring
cin
parents:
diff changeset
13
240aa6994018 component model refactoring
cin
parents:
diff changeset
14 public event EventHandler Disposed;
240aa6994018 component model refactoring
cin
parents:
diff changeset
15
240aa6994018 component model refactoring
cin
parents:
diff changeset
16 public bool IsDisposed {
240aa6994018 component model refactoring
cin
parents:
diff changeset
17 get {
240aa6994018 component model refactoring
cin
parents:
diff changeset
18 Thread.MemoryBarrier();
240aa6994018 component model refactoring
cin
parents:
diff changeset
19 return m_disposed != 0;
240aa6994018 component model refactoring
cin
parents:
diff changeset
20 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
21 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
22
240aa6994018 component model refactoring
cin
parents:
diff changeset
23 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
24 /// Asserts the object is not disposed.
240aa6994018 component model refactoring
cin
parents:
diff changeset
25 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
26 /// <exception cref="ObjectDisposedException">The object is disposed</exception>
240aa6994018 component model refactoring
cin
parents:
diff changeset
27 /// <remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
28 /// Успешная проверка того, что объект не освобожден еще не гарантирует, что он не
240aa6994018 component model refactoring
cin
parents:
diff changeset
29 /// будет освобожден сразу после нее, поэтому методы использующие проверку должны
240aa6994018 component model refactoring
cin
parents:
diff changeset
30 /// учитывать, что объект может быть освобожден из параллельного потока.
240aa6994018 component model refactoring
cin
parents:
diff changeset
31 /// Данны метод служит для упрощения отладки ошибок при использовании объекта после его
240aa6994018 component model refactoring
cin
parents:
diff changeset
32 /// освобождения.
240aa6994018 component model refactoring
cin
parents:
diff changeset
33 /// </remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
34 /// <example>
240aa6994018 component model refactoring
cin
parents:
diff changeset
35 /// // пример синхронизированного освобождения ресурсов
240aa6994018 component model refactoring
cin
parents:
diff changeset
36 /// class FileStore : Disposable {
240aa6994018 component model refactoring
cin
parents:
diff changeset
37 /// readonly TextWriter m_file;
240aa6994018 component model refactoring
cin
parents:
diff changeset
38 /// readonly obejct m_sync = new object();
240aa6994018 component model refactoring
cin
parents:
diff changeset
39 ///
240aa6994018 component model refactoring
cin
parents:
diff changeset
40 /// public FileStore(string file) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
41 /// m_file = new TextWriter(File.OpenWrite(file));
240aa6994018 component model refactoring
cin
parents:
diff changeset
42 /// }
240aa6994018 component model refactoring
cin
parents:
diff changeset
43 ///
240aa6994018 component model refactoring
cin
parents:
diff changeset
44 /// public void Write(string text) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
45 /// lock(m_sync) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
46 /// AssertNotDisposed();
240aa6994018 component model refactoring
cin
parents:
diff changeset
47 /// m_file.Write(text);
240aa6994018 component model refactoring
cin
parents:
diff changeset
48 /// }
240aa6994018 component model refactoring
cin
parents:
diff changeset
49 /// }
240aa6994018 component model refactoring
cin
parents:
diff changeset
50 ///
240aa6994018 component model refactoring
cin
parents:
diff changeset
51 /// protected override void Dispose(bool disposing) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
52 /// if (disposing)
240aa6994018 component model refactoring
cin
parents:
diff changeset
53 /// lock(m_sync) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
54 /// m_file.Dipose();
240aa6994018 component model refactoring
cin
parents:
diff changeset
55 /// base.Dispose(true);
240aa6994018 component model refactoring
cin
parents:
diff changeset
56 /// }
240aa6994018 component model refactoring
cin
parents:
diff changeset
57 /// else
240aa6994018 component model refactoring
cin
parents:
diff changeset
58 /// base.Dispose(false);
240aa6994018 component model refactoring
cin
parents:
diff changeset
59 /// }
240aa6994018 component model refactoring
cin
parents:
diff changeset
60 /// }
240aa6994018 component model refactoring
cin
parents:
diff changeset
61 /// <example>
240aa6994018 component model refactoring
cin
parents:
diff changeset
62 protected void AssertNotDisposed() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
63 Thread.MemoryBarrier();
240aa6994018 component model refactoring
cin
parents:
diff changeset
64 if (m_disposed != 0)
240aa6994018 component model refactoring
cin
parents:
diff changeset
65 throw new ObjectDisposedException(ToString());
240aa6994018 component model refactoring
cin
parents:
diff changeset
66 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
67 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
68 /// Вызывает событие <see cref="Disposed"/>
240aa6994018 component model refactoring
cin
parents:
diff changeset
69 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
70 /// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод
240aa6994018 component model refactoring
cin
parents:
diff changeset
71 /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого
240aa6994018 component model refactoring
cin
parents:
diff changeset
72 /// объекта.</param>
240aa6994018 component model refactoring
cin
parents:
diff changeset
73 /// <remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
74 /// Данный метод вызывается гарантированно один раз даже при одновременном вызове <see cref="Dispose()"/>
240aa6994018 component model refactoring
cin
parents:
diff changeset
75 /// из нескольких потоков.
240aa6994018 component model refactoring
cin
parents:
diff changeset
76 /// </remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
77 protected virtual void Dispose(bool disposing) {
213
9ee78a345738 Minor code changes
cin
parents: 208
diff changeset
78 if (disposing)
9ee78a345738 Minor code changes
cin
parents: 208
diff changeset
79 Disposed.DispatchEvent(this, EventArgs.Empty);
9ee78a345738 Minor code changes
cin
parents: 208
diff changeset
80
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
81 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
82
208
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 152
diff changeset
83 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
84 public void Dispose() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
85 if (Interlocked.Increment(ref m_disposed) == 1) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
86 Dispose(true);
240aa6994018 component model refactoring
cin
parents:
diff changeset
87 GC.SuppressFinalize(this);
240aa6994018 component model refactoring
cin
parents:
diff changeset
88 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
89 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
90
240aa6994018 component model refactoring
cin
parents:
diff changeset
91 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
92 /// Записывает сообщение об утечке объекта.
240aa6994018 component model refactoring
cin
parents:
diff changeset
93 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
94 protected virtual void ReportObjectLeaks() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
95 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
240aa6994018 component model refactoring
cin
parents:
diff changeset
96 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
97
240aa6994018 component model refactoring
cin
parents:
diff changeset
98 ~Disposable() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
99 Dispose(false);
240aa6994018 component model refactoring
cin
parents:
diff changeset
100 ReportObjectLeaks();
240aa6994018 component model refactoring
cin
parents:
diff changeset
101 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
102 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
103 }