Background
When developing a 3G application, the program exits and the opened link needs to be closed automatically. In this way, you need to dispose all the resources allocated by Winform when exiting. This article describes several methods for Winform Dispose resources.
Solution 1
If you use VS2005 or later for Winform development, Visual Studio will automatically generate a partial class (usually called *) used to save layout information and process events *. designer. cs) This partial class reloads the Dispose method.
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
However, this partial class is automatically generated by Visual Studio and should not be modified manually. The simplest way to Dispose is to copy this method to another class file. It is generally *. cs, and then add the code that requires Dispose.
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
DisposeResources();
base.Dispose(disposing);
}
Solution 2
Register the Disposed event.
this.Disposed += new EventHandler(MainForm_Disposed);
void MainForm_Disposed(object sender, EventArgs e)
{
Logger.Instance.LogTrace("MainForm_Disposed");
}
When Dispose calls the following code, it calls the registered event processing function.
if (disposing && (components != null))
{
components.Dispose();
}
However, this method has a problem. If the Form does not have any other components, MainForm_Disposed will not be called, so there is solution 3 below.
Solution 3
Due to the problem of solution 2, solution 3 is proposed. Solution 3 is to use a class Disposer that inherits from Component. The Disposer class saves the reference of the class that requires Dispose, and then adds the Disposer class to components.
internal class Disposer : Component
{
private IDisposable _disposable;
internal Disposer(IDisposable disposable)
{
_disposable = disposable;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_disposable != null)
{
_disposable.Dispose();
}
}
base.Dispose(disposing);
}
}
Defines a class Disposer that inherits from Component. The Disposer saves the reference of the class that requires Dispose.
components.Add(new Disposer(connectionManager));
Save the Disposer object to components, so that components cannot be null. The following code is executed.
if (disposing && (components != null))
{
components.Dispose();
}
ConnectionManager is a member that requires Dispose. The class of this object must inherit IDisposable and overload Dispose.
sealed class ConnectionManager : IDisposable
{
public void Dispose()
{
//Disconnect the network
}
}
Solution 3 is complete, with merry chrismas.