Mercurial > pub > ImplabNet
annotate Implab/Components/Disposable.cs @ 254:12c00235b105 v3
Добавлена метка v3.0.1-beta для набора изменений 34df34841225
author | cin |
---|---|
date | Mon, 12 Feb 2018 17:03:49 +0300 |
parents | 6f4630d0bcd9 |
children |
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 ~Disposable() { | |
52 Dispose(false); | |
53 } | |
54 } | |
55 } |