Question

I've read about IDisposable pattern on this article and want to implement it in my windows form application. As we know that in windows form .Designer.cs class there is already Dispose method

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

and in .cs class I'm using Typed Dataset to read and save the data.

public partial class frmCustomerList
{
    private MyTypedDataSet ds = new MyTypedDataSet();
    ...
}

So, how to implement IDisposable to dispose MyTypedDataSet? If I implement IDisposable in frmCustomerList and implement its interface

public partial class frmCustomerList : IDisposable
{
    private MyTypedDataSet ds = new MyTypedDataSet();
    void Dispose()
    {
       ds.Dispose();
    }
}

what about Dispose(bool disposing) method in .Designer.cs?

Était-ce utile?

La solution 2

I would dispose any members of the form using one of the forms events such as

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosed(v=vs.110).aspx

e.g

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);

    if (ds != null)
        ds.Dispose();
}

Autres conseils

If you look in the Designer.cs file and look below the dispose method you will see this

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

Only InializeComponent() is warned about not modifing. You can cut (not copy) and paste the protected override void Dispose(bool disposing) out of the designer file and move it in your main code file without worry, just be sure to leave the components.Dispose(); part in as any disposable objects you add through the designer will be put in that collection for disposing.

public partial class frmCustomerList
{
    private MyTypedDataSet ds = new MyTypedDataSet();

    protected override void Dispose(bool disposing)
    {
        ds.Dispose();

        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    //The rest of your frmCustomerList.cs file.
}

I think you must not care about disposing your class except if you have unmanaged resources. Here is an example where it actually useful:

public class ComplexResourceHolder : IDisposable
{

    private IntPtr buffer; // unmanaged memory buffer
    private SafeHandle resource; // disposable handle to a resource

    public IntPtr Buffer { get { return buffer; } set { buffer = value; } }

    public ComplexResourceHolder()
    {
        this.buffer = ... // allocates memory
        this.resource = ... // allocates the resource
    }

    protected virtual void Dispose(bool disposing)
    {
        ReleaseBuffer(buffer); // release unmanaged memory
    if (disposing)
    { 
        // release other disposable objects
        if (resource!= null)
           resource.Dispose();
    }
}

~ ComplexResourceHolder(){
    Dispose(false);
}

public void Dispose(){
    Dispose(true);
    GC.SuppressFinalize(this);
}

}

Check the MSDN for better understanding of Dispose(bool) finalizer override. And this link about unmanaged resources will be useful as well because this is the first reason why you should use IDisposable.

You might wonder to use constructions like below if the class inherits IDisposable:

using (ComplexResourceHolder crh = new ComplexResourceHolder())
{
    //Do something with buffer for an instance
    //crh.Buffer =
}

Dispose method will be called automatically after closing tag '}'.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top