using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MultiTheftAuto.Utils { /// /// Defines methods to release allocated resources and to check whether this resource has been disposed. /// public abstract class Disposable : IDisposable { /// /// Gets whether this resource has been disposed. /// public bool Disposed { get; private set; } /// /// Performs tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { if( Disposed ) { //We've been desposed already. Abort further disposure. return; } //Dispose all native and managed resources. Dispose( true ); //Remember we've been disposed. Disposed = true; //Suppress finalisation; We already disposed our resources. GC.SuppressFinalize( this ); } ~Disposable() { if( Disposed ) { //We've been desposed already. Abort further disposure. return; } Dispose( false ); //We don't care to set Disposed value; Resource is being collected by GC anyways. } protected void CheckDisposure() { if( Disposed ) { throw new ObjectDisposedException( "Cannot access disposed resource." ); } } /// /// Performs tasks associated with freeing, releasing, or resetting unmanaged resources. /// /// Whether managed resources should be disposed. protected abstract void Dispose( bool disposing ); } }