annotate Implab/Components/Disposable.cs @ 251:7c7e9ad6fe4a v3

Prerelease version of RunnableComponent Added draft messaging interfaces Added more more helpers to Xml/SerializationHelpers
author cin
date Sun, 11 Feb 2018 00:49:51 +0300
parents 9f63dade3a40
children 6f4630d0bcd9
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 public event EventHandler Disposed;
240aa6994018 component model refactoring
cin
parents:
diff changeset
13
240aa6994018 component model refactoring
cin
parents:
diff changeset
14 public bool IsDisposed {
250
9f63dade3a40 Working on runnable component
cin
parents: 213
diff changeset
15 get; private set;
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
16 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
17
240aa6994018 component model refactoring
cin
parents:
diff changeset
18 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
19 /// Asserts the object is not disposed.
240aa6994018 component model refactoring
cin
parents:
diff changeset
20 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
21 /// <exception cref="ObjectDisposedException">The object is disposed</exception>
240aa6994018 component model refactoring
cin
parents:
diff changeset
22 /// <remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
23 protected void AssertNotDisposed() {
250
9f63dade3a40 Working on runnable component
cin
parents: 213
diff changeset
24 if (IsDisposed)
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
25 throw new ObjectDisposedException(ToString());
240aa6994018 component model refactoring
cin
parents:
diff changeset
26 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
27 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
28 /// Вызывает событие <see cref="Disposed"/>
240aa6994018 component model refactoring
cin
parents:
diff changeset
29 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
30 /// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод
240aa6994018 component model refactoring
cin
parents:
diff changeset
31 /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого
240aa6994018 component model refactoring
cin
parents:
diff changeset
32 /// объекта.</param>
240aa6994018 component model refactoring
cin
parents:
diff changeset
33 /// <remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
34 /// Данный метод вызывается гарантированно один раз даже при одновременном вызове <see cref="Dispose()"/>
240aa6994018 component model refactoring
cin
parents:
diff changeset
35 /// из нескольких потоков.
240aa6994018 component model refactoring
cin
parents:
diff changeset
36 /// </remarks>
240aa6994018 component model refactoring
cin
parents:
diff changeset
37 protected virtual void Dispose(bool disposing) {
213
9ee78a345738 Minor code changes
cin
parents: 208
diff changeset
38 if (disposing)
9ee78a345738 Minor code changes
cin
parents: 208
diff changeset
39 Disposed.DispatchEvent(this, EventArgs.Empty);
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
40 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
41
208
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 152
diff changeset
42 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
43 public void Dispose() {
250
9f63dade3a40 Working on runnable component
cin
parents: 213
diff changeset
44 if(!IsDisposed) {
9f63dade3a40 Working on runnable component
cin
parents: 213
diff changeset
45 IsDisposed = true;
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
46 Dispose(true);
240aa6994018 component model refactoring
cin
parents:
diff changeset
47 GC.SuppressFinalize(this);
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 /// <summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
52 /// Записывает сообщение об утечке объекта.
240aa6994018 component model refactoring
cin
parents:
diff changeset
53 /// </summary>
240aa6994018 component model refactoring
cin
parents:
diff changeset
54 protected virtual void ReportObjectLeaks() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
55 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
240aa6994018 component model refactoring
cin
parents:
diff changeset
56 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
57
240aa6994018 component model refactoring
cin
parents:
diff changeset
58 ~Disposable() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
59 Dispose(false);
240aa6994018 component model refactoring
cin
parents:
diff changeset
60 ReportObjectLeaks();
240aa6994018 component model refactoring
cin
parents:
diff changeset
61 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
62 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
63 }