How to Dispose resources when Windows Mobile uses. NET Compact Framework to develop Winform

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.