Public class BaseDisposable: IDisposable
{
~ BaseDisposable ()
{
// The Garbage Collector calls this method, so the parameter must be false.
Dispose (false );
}
/// <Summary>
/// Whether the Dispose (bool disposing) method has been called.
/// Should be defined as private, so that the base class and subclass do not affect each other.
/// </Summary>
Private bool disposed = false;
/// <Summary>
/// This method is used to complete all recycling tasks.
/// Subclass should overwrite this method.
/// </Summary>
/// <Param name = "disposing"> </param>
Protected virtual void Dispose (bool disposing)
{
// Avoid calling Dispose repeatedly.
If (! Disposed) return;
// Adapt to the multi-threaded environment to avoid thread errors.
Lock (this)
{
If (disposing)
{
//------------------------------------------------
// Write the code for releasing managed resources here
// (1) if the Dispose () method exists, call its Dispose () method.
// (2) if there is no Dispose () method, set it to null.
// Example:
// XxDataTable. Dispose ();
// XxDataAdapter. Dispose ();
// XxString = null;
//------------------------------------------------
}
//------------------------------------------------
// Write and release unmanaged resources here
// Example:
// File handle
//------------------------------------------------
Disposed = true;
}
}
/// <Summary>
/// This method is called by a program. After this method is called, the object is terminated.
/// This method is defined in the IDisposable interface.
/// </Summary>
Public void Dispose ()
{
// Because the program calls this method, the parameter is true.
Dispose (true );
// Because we do not want the Garbage Collector to terminate the object again, we need to remove the object from the end list.
GC. SuppressFinalize (this );
}
/// <Summary>
/// Call the Dispose () method to recycle resources.
/// </Summary>
Public void Close ()
{
Dispose ();
}
}