Disposable.cs
62 lines
| 2.4 KiB
| text/x-csharp
|
CSharpLexer
cin
|
r152 | using Implab.Diagnostics; | |
using System; | |||
cin
|
r208 | using System.Diagnostics.CodeAnalysis; | |
cin
|
r152 | using System.Threading; | |
namespace Implab.Components { | |||
/// <summary> | |||
/// Base class the objects which support disposing. | |||
/// </summary> | |||
public class Disposable : IDisposable { | |||
cin
|
r213 | ||
cin
|
r152 | public event EventHandler Disposed; | |
public bool IsDisposed { | |||
cin
|
r250 | get; private set; | |
cin
|
r152 | } | |
/// <summary> | |||
/// Asserts the object is not disposed. | |||
/// </summary> | |||
/// <exception cref="ObjectDisposedException">The object is disposed</exception> | |||
/// <remarks> | |||
protected void AssertNotDisposed() { | |||
cin
|
r250 | if (IsDisposed) | |
cin
|
r152 | throw new ObjectDisposedException(ToString()); | |
} | |||
/// <summary> | |||
/// Вызывает событие <see cref="Disposed"/> | |||
/// </summary> | |||
/// <param name="disposing">Признак того, что нужно освободить ресурсы, иначе данный метод | |||
/// вызван сборщиком мусора и нужно освобождать ТОЛЬКО неуправляемые ресурсы ТОЛЬКО этого | |||
/// объекта.</param> | |||
/// <remarks> | |||
/// Данный метод вызывается гарантированно один раз даже при одновременном вызове <see cref="Dispose()"/> | |||
/// из нескольких потоков. | |||
/// </remarks> | |||
protected virtual void Dispose(bool disposing) { | |||
cin
|
r213 | if (disposing) | |
Disposed.DispatchEvent(this, EventArgs.Empty); | |||
cin
|
r152 | } | |
cin
|
r208 | [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")] | |
cin
|
r152 | public void Dispose() { | |
cin
|
r250 | if(!IsDisposed) { | |
IsDisposed = true; | |||
cin
|
r152 | Dispose(true); | |
GC.SuppressFinalize(this); | |||
} | |||
} | |||
/// <summary> | |||
/// Записывает сообщение об утечке объекта. | |||
/// </summary> | |||
protected virtual void ReportObjectLeaks() { | |||
TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); | |||
} | |||
~Disposable() { | |||
Dispose(false); | |||
ReportObjectLeaks(); | |||
} | |||
} | |||
} |