Mercurial > pub > ImplabNet
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 |
rev | line source |
---|---|
152 | 1 using Implab.Diagnostics; |
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 | 4 using System.Threading; |
5 | |
6 namespace Implab.Components { | |
7 /// <summary> | |
8 /// Base class the objects which support disposing. | |
9 /// </summary> | |
10 public class Disposable : IDisposable { | |
213 | 11 |
152 | 12 public event EventHandler Disposed; |
13 | |
14 public bool IsDisposed { | |
250 | 15 get; private set; |
152 | 16 } |
17 | |
18 /// <summary> | |
19 /// Asserts the object is not disposed. | |
20 /// </summary> | |
21 /// <exception cref="ObjectDisposedException">The object is disposed</exception> | |
22 /// <remarks> | |
23 protected void AssertNotDisposed() { | |
250 | 24 if (IsDisposed) |
152 | 25 throw new ObjectDisposedException(ToString()); |
26 } | |
27 /// <summary> | |
28 /// Вызывает событие <see cref="Disposed"/> | |
29 /// </summary> | |
30 /// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод | |
31 /// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого | |
32 /// объекта.</param> | |
33 /// <remarks> | |
34 /// Данный метод вызывается гарантированно один раз даже при одновременном вызове <see cref="Dispose()"/> | |
35 /// из нескольких потоков. | |
36 /// </remarks> | |
37 protected virtual void Dispose(bool disposing) { | |
213 | 38 if (disposing) |
39 Disposed.DispatchEvent(this, EventArgs.Empty); | |
152 | 40 } |
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 | 43 public void Dispose() { |
250 | 44 if(!IsDisposed) { |
45 IsDisposed = true; | |
152 | 46 Dispose(true); |
47 GC.SuppressFinalize(this); | |
48 } | |
49 } | |
50 | |
51 /// <summary> | |
52 /// Записывает сообщение об утечке объекта. | |
53 /// </summary> | |
54 protected virtual void ReportObjectLeaks() { | |
55 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); | |
56 } | |
57 | |
58 ~Disposable() { | |
59 Dispose(false); | |
60 ReportObjectLeaks(); | |
61 } | |
62 } | |
63 } |