when to dispose a datatable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smokey Grindle

    #1

    when to dispose a datatable?

    I have a list box on my form, but I need to databind it to a data table that
    is a private member of the form's class... so I basically have

    Public Class MyForm
    priate m_MyTable as new datatable
    End Class

    now where would I properly dispose of this? In the finalize method? I am
    loading the data for the table in a subroutine that is executed at form
    load, of course all the commands tied to it are wrapped in using blocks, but
    I cant wrap the table in that because it i put it in a using block then
    databind it, of course it will disappear when it exits that block and then
    the databinding is gone, so I made it at the class level instead and want
    its life time to be that of the form.. thanks!


  • AlexS

    #2
    Re: when to dispose a datatable?

    Dispose it when you don't need table anymore.

    You can do that in FormClosing or in form Dispose if you implement it

    HTH
    "Smokey Grindle" <nospam@nospam. comwrote in message
    news:uMyeIWCrHH A.1212@TK2MSFTN GP05.phx.gbl...
    >I have a list box on my form, but I need to databind it to a data table
    >that is a private member of the form's class... so I basically have
    >
    Public Class MyForm
    priate m_MyTable as new datatable
    End Class
    >
    now where would I properly dispose of this? In the finalize method? I am
    loading the data for the table in a subroutine that is executed at form
    load, of course all the commands tied to it are wrapped in using blocks,
    but I cant wrap the table in that because it i put it in a using block
    then databind it, of course it will disappear when it exits that block and
    then the databinding is gone, so I made it at the class level instead and
    want its life time to be that of the form.. thanks!
    >
    >

    Comment

    • Miha Markic

      #3
      Re: when to dispose a datatable?

      Just a note that you don't actually need to dispose datatable, but it is a
      good practice.
      And yes, dispose it when you are done with it (form's dispose might be a
      good place if you can't dispose it earlier)

      --
      Miha Markic [MVP C#, INETA Country Leader for Slovenia]
      RightHand .NET consulting & development www.rthand.com
      Blog: http://cs.rthand.com/blogs/blog_with_righthand/

      "Smokey Grindle" <nospam@nospam. comwrote in message
      news:uMyeIWCrHH A.1212@TK2MSFTN GP05.phx.gbl...
      >I have a list box on my form, but I need to databind it to a data table
      >that is a private member of the form's class... so I basically have
      >
      Public Class MyForm
      priate m_MyTable as new datatable
      End Class
      >
      now where would I properly dispose of this? In the finalize method? I am
      loading the data for the table in a subroutine that is executed at form
      load, of course all the commands tied to it are wrapped in using blocks,
      but I cant wrap the table in that because it i put it in a using block
      then databind it, of course it will disappear when it exits that block and
      then the databinding is gone, so I made it at the class level instead and
      want its life time to be that of the form.. thanks!
      >

      Comment

      • Smokey Grindle

        #4
        Re: when to dispose a datatable?

        what about in a custom control? should it be in the dispose method or
        finalize method? I remember this is a difference between them, i believe
        that dispose is immediate and finalize can happen at any time after the
        object has fallen out of scope?

        "Miha Markic" <miha at rthand comwrote in message
        news:0778343A-0880-4A8C-B412-EEB6D3CCF267@mi crosoft.com...
        Just a note that you don't actually need to dispose datatable, but it is a
        good practice.
        And yes, dispose it when you are done with it (form's dispose might be a
        good place if you can't dispose it earlier)
        >
        --
        Miha Markic [MVP C#, INETA Country Leader for Slovenia]
        RightHand .NET consulting & development www.rthand.com
        Blog: http://cs.rthand.com/blogs/blog_with_righthand/
        >
        "Smokey Grindle" <nospam@nospam. comwrote in message
        news:uMyeIWCrHH A.1212@TK2MSFTN GP05.phx.gbl...
        >>I have a list box on my form, but I need to databind it to a data table
        >>that is a private member of the form's class... so I basically have
        >>
        >Public Class MyForm
        > priate m_MyTable as new datatable
        >End Class
        >>
        >now where would I properly dispose of this? In the finalize method? I am
        >loading the data for the table in a subroutine that is executed at form
        >load, of course all the commands tied to it are wrapped in using blocks,
        >but I cant wrap the table in that because it i put it in a using block
        >then databind it, of course it will disappear when it exits that block
        >and then the databinding is gone, so I made it at the class level instead
        >and want its life time to be that of the form.. thanks!
        >>
        >

        Comment

        • Miha Markic

          #5
          Re: when to dispose a datatable?

          Finalize is a place for disposing *unmanaged* controls.
          Furthermore, controls placed on form are disposed automatically when form is
          disposed, so no action is required.

          --
          Miha Markic [MVP C#, INETA Country Leader for Slovenia]
          RightHand .NET consulting & development www.rthand.com
          Blog: http://cs.rthand.com/blogs/blog_with_righthand/

          "Smokey Grindle" <nospam@nospam. comwrote in message
          news:eVDLCxDrHH A.4984@TK2MSFTN GP03.phx.gbl...
          what about in a custom control? should it be in the dispose method or
          finalize method? I remember this is a difference between them, i believe
          that dispose is immediate and finalize can happen at any time after the
          object has fallen out of scope?
          >
          "Miha Markic" <miha at rthand comwrote in message
          news:0778343A-0880-4A8C-B412-EEB6D3CCF267@mi crosoft.com...
          >Just a note that you don't actually need to dispose datatable, but it is
          >a good practice.
          >And yes, dispose it when you are done with it (form's dispose might be a
          >good place if you can't dispose it earlier)
          >>
          >--
          >Miha Markic [MVP C#, INETA Country Leader for Slovenia]
          >RightHand .NET consulting & development www.rthand.com
          >Blog: http://cs.rthand.com/blogs/blog_with_righthand/
          >>
          >"Smokey Grindle" <nospam@nospam. comwrote in message
          >news:uMyeIWCrH HA.1212@TK2MSFT NGP05.phx.gbl.. .
          >>>I have a list box on my form, but I need to databind it to a data table
          >>>that is a private member of the form's class... so I basically have
          >>>
          >>Public Class MyForm
          >> priate m_MyTable as new datatable
          >>End Class
          >>>
          >>now where would I properly dispose of this? In the finalize method? I am
          >>loading the data for the table in a subroutine that is executed at form
          >>load, of course all the commands tied to it are wrapped in using blocks,
          >>but I cant wrap the table in that because it i put it in a using block
          >>then databind it, of course it will disappear when it exits that block
          >>and then the databinding is gone, so I made it at the class level
          >>instead and want its life time to be that of the form.. thanks!
          >>>
          >>
          >
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: when to dispose a datatable?

            Miha,

            Not only on forms, this is as well for components or whereever there where
            Idisposible is nice implemented.

            I assume you knew this, so for the OP.

            Cor

            "Miha Markic" <miha at rthand comschreef in bericht
            news:1C7336E1-B770-4A10-8545-BE13AFDBA6BA@mi crosoft.com...
            Finalize is a place for disposing *unmanaged* controls.
            Furthermore, controls placed on form are disposed automatically when form
            is disposed, so no action is required.
            >
            --
            Miha Markic [MVP C#, INETA Country Leader for Slovenia]
            RightHand .NET consulting & development www.rthand.com
            Blog: http://cs.rthand.com/blogs/blog_with_righthand/
            >
            "Smokey Grindle" <nospam@nospam. comwrote in message
            news:eVDLCxDrHH A.4984@TK2MSFTN GP03.phx.gbl...
            >what about in a custom control? should it be in the dispose method or
            >finalize method? I remember this is a difference between them, i believe
            >that dispose is immediate and finalize can happen at any time after the
            >object has fallen out of scope?
            >>
            >"Miha Markic" <miha at rthand comwrote in message
            >news:0778343 A-0880-4A8C-B412-EEB6D3CCF267@mi crosoft.com...
            >>Just a note that you don't actually need to dispose datatable, but it is
            >>a good practice.
            >>And yes, dispose it when you are done with it (form's dispose might be a
            >>good place if you can't dispose it earlier)
            >>>
            >>--
            >>Miha Markic [MVP C#, INETA Country Leader for Slovenia]
            >>RightHand .NET consulting & development www.rthand.com
            >>Blog: http://cs.rthand.com/blogs/blog_with_righthand/
            >>>
            >>"Smokey Grindle" <nospam@nospam. comwrote in message
            >>news:uMyeIWCr HHA.1212@TK2MSF TNGP05.phx.gbl. ..
            >>>>I have a list box on my form, but I need to databind it to a data table
            >>>>that is a private member of the form's class... so I basically have
            >>>>
            >>>Public Class MyForm
            >>> priate m_MyTable as new datatable
            >>>End Class
            >>>>
            >>>now where would I properly dispose of this? In the finalize method? I
            >>>am loading the data for the table in a subroutine that is executed at
            >>>form load, of course all the commands tied to it are wrapped in using
            >>>blocks, but I cant wrap the table in that because it i put it in a
            >>>using block then databind it, of course it will disappear when it exits
            >>>that block and then the databinding is gone, so I made it at the class
            >>>level instead and want its life time to be that of the form.. thanks!
            >>>>
            >>>
            >>
            >>
            >

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: when to dispose a datatable?

              Smokey,

              In the Data namespace there is only one thing that can be disposed, which is
              up to you and that is the connection. As you use the using clause it is even
              astatically disposed.

              Dispose is not (read not) the same as finalizing, you are using managed
              code, it is setting the object ready for removing by the GC. Some people
              think that how quicker they do that how better. If there is no need for
              extra resources, it is the opposite. So let your managed code do the work,
              and interfere not in that what the developers by Microsoft made for you.

              Cor

              "Smokey Grindle" <nospam@nospam. comschreef in bericht
              news:uMyeIWCrHH A.1212@TK2MSFTN GP05.phx.gbl...
              >I have a list box on my form, but I need to databind it to a data table
              >that is a private member of the form's class... so I basically have
              >
              Public Class MyForm
              priate m_MyTable as new datatable
              End Class
              >
              now where would I properly dispose of this? In the finalize method? I am
              loading the data for the table in a subroutine that is executed at form
              load, of course all the commands tied to it are wrapped in using blocks,
              but I cant wrap the table in that because it i put it in a using block
              then databind it, of course it will disappear when it exits that block and
              then the databinding is gone, so I made it at the class level instead and
              want its life time to be that of the form.. thanks!
              >

              Comment

              • Miha Markic

                #8
                Re: when to dispose a datatable?


                "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                news:%23wB6koEr HHA.4984@TK2MSF TNGP03.phx.gbl. ..
                Smokey,
                >
                In the Data namespace there is only one thing that can be disposed, which
                is up to you and that is the connection. As you use the using clause it is
                even astatically disposed.
                >
                Dispose is not (read not) the same as finalizing, you are using managed
                code, it is setting the object ready for removing by the GC.
                No, Dispose doesn't affect the GC for the instance where Dispose is called
                in any way whatsoever.
                IOW if you call someobject.Disp ose() it won't affect the GC for someobject.

                Some people
                think that how quicker they do that how better. If there is no need for
                extra resources, it is the opposite. So let your managed code do the work,
                and interfere not in that what the developers by Microsoft made for you.
                I assume you are talking about Data namespace where Dispose usually does not
                very much.
                However, in general, Dispose has to be called to release resources.
                Furthermore even on classes that doesn't handle unmanaged code directly the
                Dispose might be of help. It might set references to other objects to null
                thus leaving those objects for garbage collection.
                Basically I recommended to call Dispose always when IDisposable is involved.
                Even if you know that Dispose does nothing the situation might change in the
                future.
                --
                Miha Markic [MVP C#, INETA Country Leader for Slovenia]
                RightHand .NET consulting & development www.rthand.com
                Blog: http://cs.rthand.com/blogs/blog_with_righthand/

                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: when to dispose a datatable?

                  Miha,

                  Even if you know that Dispose does nothing the situation might change in
                  the future.
                  Yea the same with the + sign, maybe it subtracts in future,

                  Both the VB and the C# team take a lot of time to see that there are no
                  breaking change, they surely won't do that in the case of the Dispose.

                  Cor

                  --
                  Miha Markic [MVP C#, INETA Country Leader for Slovenia]
                  RightHand .NET consulting & development www.rthand.com
                  Blog: http://cs.rthand.com/blogs/blog_with_righthand/

                  Comment

                  • Miha Markic

                    #10
                    Re: when to dispose a datatable?

                    Both the VB and the C# team take a lot of time to see that there are no
                    breaking change, they surely won't do that in the case of the Dispose.
                    What exactly have language teams with BCL implementation (IOW Dispose
                    implementation) ? And what do they have with 3rd party software Dispose
                    implementation?
                    You are certainly not saying that language teams control Dispose
                    implementation of all classes on the world, do you?
                    --
                    Miha Markic [MVP C#, INETA Country Leader for Slovenia]
                    RightHand .NET consulting & development www.rthand.com
                    Blog: http://cs.rthand.com/blogs/blog_with_righthand/

                    Comment

                    • Jack Jackson

                      #11
                      Re: when to dispose a datatable?

                      On Mon, 11 Jun 2007 19:04:42 +0200, "Cor Ligthert [MVP]"
                      <notmyfirstname @planet.nlwrote :
                      >Smokey,
                      >
                      >In the Data namespace there is only one thing that can be disposed, which is
                      >up to you and that is the connection. As you use the using clause it is even
                      >astatically disposed.
                      How do you know this? Is it documented anywhere?

                      I have been disposing all of the Data namespace items because I didn't
                      know if I needed to to not.

                      Comment

                      • rowe_newsgroups

                        #12
                        Re: when to dispose a datatable?

                        On Jun 12, 1:38 pm, "Miha Markic" <miha at rthand comwrote:
                        Both the VB and the C# team take a lot of time to see that there are no
                        breaking change, they surely won't do that in the case of the Dispose.
                        >
                        What exactly have language teams with BCL implementation (IOW Dispose
                        implementation) ? And what do they have with 3rd party software Dispose
                        implementation?
                        You are certainly not saying that language teams control Dispose
                        implementation of all classes on the world, do you?
                        --
                        Miha Markic [MVP C#, INETA Country Leader for Slovenia]
                        RightHand .NET consulting & developmentwww. rthand.com
                        Blog:http://cs.rthand.com/blogs/blog_with_righthand/
                        Miha - just as a warning - I along with many others have been down
                        this path with Cor quite a few times to no avail. You're welcome to
                        keep trying but I doubt anything will change.

                        Thanks,

                        Seth Rowe

                        Comment

                        • Miha Markic

                          #13
                          Re: when to dispose a datatable?

                          Miha - just as a warning - I along with many others have been down
                          this path with Cor quite a few times to no avail. You're welcome to
                          keep trying but I doubt anything will change.
                          :-) Yes, yes, I know. I am actually not trying to go that path. I just can't
                          let false statements about disposing flow here.
                          There is already so much Dispose/GC misunderstandin gs all over the
                          community...

                          --
                          Miha Markic [MVP C#, INETA Country Leader for Slovenia]
                          RightHand .NET consulting & development www.rthand.com
                          Blog: http://cs.rthand.com/blogs/blog_with_righthand/

                          Comment

                          • Brian Gideon

                            #14
                            Re: when to dispose a datatable?

                            On Jun 11, 12:04 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                            wrote:
                            Dispose is not (read not) the same as finalizing, you are using managed
                            code, it is setting the object ready for removing by the GC. Some people
                            think that how quicker they do that how better. If there is no need for
                            extra resources, it is the opposite. So let your managed code do the work,
                            and interfere not in that what the developers by Microsoft made for you.
                            In most cases it is imperative to call Dispose as soon as the object
                            is no longer needed. An argument could be made that a DataTable is an
                            excpetion to that rule, but a FileHandle, IDbConnection, and the like
                            are not.

                            The canonical implementation of Dispose will only prevent the GC from
                            calling Finalize via GC.SuppressFina lize. Other than that the garbage
                            collection process is same as with any other class.

                            Also, are you saying that it is better to avoid calling Dispose?

                            Brian

                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: when to dispose a datatable?

                              Brian,

                              I am not saying it is better, I see this however often in relation with
                              cleaning memory.

                              Cleaning memory takes time and gives nothing back. So let it be done in the
                              proper time, when there is nothing else to be done, something the GC is
                              standard doing.

                              Cor

                              "Brian Gideon" <briangideon@ya hoo.comschreef in bericht
                              news:1181699488 .917917.222300@ g37g2000prf.goo glegroups.com.. .
                              On Jun 11, 12:04 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                              wrote:
                              >Dispose is not (read not) the same as finalizing, you are using managed
                              >code, it is setting the object ready for removing by the GC. Some people
                              >think that how quicker they do that how better. If there is no need for
                              >extra resources, it is the opposite. So let your managed code do the
                              >work,
                              >and interfere not in that what the developers by Microsoft made for you.
                              >
                              In most cases it is imperative to call Dispose as soon as the object
                              is no longer needed. An argument could be made that a DataTable is an
                              excpetion to that rule, but a FileHandle, IDbConnection, and the like
                              are not.
                              >
                              The canonical implementation of Dispose will only prevent the GC from
                              calling Finalize via GC.SuppressFina lize. Other than that the garbage
                              collection process is same as with any other class.
                              >
                              Also, are you saying that it is better to avoid calling Dispose?
                              >
                              Brian
                              >

                              Comment

                              Working...