using System; namespace SharpDX { /// /// Base class for a class. /// public abstract class DisposeBase : IDisposable { /// /// Occurs when this instance is starting to be disposed. /// public event EventHandler Disposing; /// /// Occurs when this instance is fully disposed. /// public event EventHandler Disposed; /// /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// ~DisposeBase() { // Finalizer calls Dispose(false) CheckAndDispose(false); } /// /// Gets a value indicating whether this instance is disposed. /// /// /// true if this instance is disposed; otherwise, false. /// public bool IsDisposed { get; private set; } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { CheckAndDispose(true); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// private void CheckAndDispose(bool disposing) { // TODO Should we throw an exception if this method is called more than once? if (!IsDisposed) { EventHandler disposingHandlers = Disposing; if (disposingHandlers != null) disposingHandlers(this, DisposeEventArgs.Get(disposing)); Dispose(disposing); GC.SuppressFinalize(this); IsDisposed = true; EventHandler disposedHandlers = Disposed; if (disposedHandlers != null) disposedHandlers(this, DisposeEventArgs.Get(disposing)); } } /// /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected abstract void Dispose(bool disposing); } }