Dispose question

I have a form with a private member that implement idisposable. Therefore i need to dipose the members.

But the dispose code is generated by the designer.

How can i dispose the member

The way i did was to handle the Form_Closing .. and in that handler .. i call the dispose

.... is there another way that is better/cleaner/good practice


Answer this question

Dispose question

  • Yonglun Li

    Put it in Form's Despose Method in Form.Designer.cs file. So whenever form is disposing it'll dispose what you want to.

    Best Regards,



  • noni78

    Hmm the File is not regenerated each time its only changed. Atleaset in all this. that dispose part is not affected and you dont need to re write your code whenever you chnage the Form's properties in designer or add or remove controls from the Form.

    I know this because I did the same in one of my projects and I was closing a Socket and disposing it in Forms's Dispose method.

    Give it a try and see I'm true.

    Best Regards,



  • ricky1980

    Maybe .. i should make a wrapper/facade .. that implements IComponent .... that wraps the other object .... hmmm .... what you guys think

  • protovision

    Unless someone comes up with a better solution...that is what I would do.


  • deodorant2

     GoDaddy wrote:
    I have a form with a private member that implement idisposable. Therefore i need to dipose the members.

    But the dispose code is generated by the designer.

    How can i dispose the member

    The way i did was to handle the Form_Closing .. and in that handler .. i call the dispose

    .... is there another way that is better/cleaner/good practice

    Since your item sounds like it is not a control, for a control falls into the Forms.Controls and during dispose the all controls on the container are disposed of properly....What you can do is add your item to the form's container list, so it is disposed of properly. For example let us say that you have a timer, one can add it to the list such as:



    this.components = new Container();
    this.myTimer = new System.Windows.Forms.Timer(this.components);

     


    Hence when dispose is called on the winform, the components will be looked at and since it exists, all items dispose will be called.



  • mariso

    If you want to delve deeper in to dispose...here is an excellent article Dispose, Finalization, and Resource Management by Joe Duffy.


  • Sam_dia

    OmegaMan wrote:
    GoDaddy wrote:
    I have a form with a private member that implement idisposable. Therefore i need to dipose the members.

    But the dispose code is generated by the designer.

    How can i dispose the member

    The way i did was to handle the Form_Closing .. and in that handler .. i call the dispose

    .... is there another way that is better/cleaner/good practice

    Since your item sounds like it is not a control, for a control falls into the Forms.Controls and during dispose the all controls on the container are disposed of properly....What you can do is add your item to the form's container list, so it is disposed of properly. For example let us say that you have a timer, one can add it to the list such as:



    this.components = new Container();
    this.myTimer = new System.Windows.Forms.Timer(this.components);



    Hence when dispose is called on the winform, the components will be looked at and since it exists, all items dispose will be called.



    The thing is that it's just an object that implements IDisposable. and i can't change the code of that class.

    to add it to the container .. it must implement IComponent .. since i cannot change the code of the object implementing IDisposable .. i cannot add it to the container of the form.



  • Laurent87471

    RizwanSharp wrote:

    Put it in Form's Despose Method in Form.Designer.cs file. So whenever form is disposing it'll dispose what you want to.

    Best Regards,




    I think that the file of the designer is regenerated ..... so the code will eventually be deleted.....

    So i don't think that i can put it in the desinger.cs file.

  • Dispose question