Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows Forms: how to do automatic disposing? Here is my answer...

Windows Forms: how to do automatic disposing? Here is my answer...

Scheduled Pinned Locked Moved C#
questionwinformsperformancetutorialannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sgatto159
    wrote on last edited by
    #1

    Hi all, here's a question for you. What if one needs to do automatic disposing of a form's components when it is closed ? And what if one wan't write Dispose() for each component ? Here is my answer, I hope you agree is correct. I tested it in win2k/winXp with taskmanager and I have seen this piece of code effectively release the memory when the form is closed. Suggestions are really appreciated./* Here f is the Form. This method is to be called in the Dispose method of the subclass of your Form. */ public static void disposeComponents(Component f) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Component) { ((Component)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }

    C S 2 Replies Last reply
    0
    • S sgatto159

      Hi all, here's a question for you. What if one needs to do automatic disposing of a form's components when it is closed ? And what if one wan't write Dispose() for each component ? Here is my answer, I hope you agree is correct. I tested it in win2k/winXp with taskmanager and I have seen this piece of code effectively release the memory when the form is closed. Suggestions are really appreciated./* Here f is the Form. This method is to be called in the Dispose method of the subclass of your Form. */ public static void disposeComponents(Component f) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Component) { ((Component)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      zu78 wrote: System.GC.Collect(); Read this[^]. In a nutshell, if you don't trust the Garbage Collector, use C++ instead ( my words, not his ). Christian Graus - Microsoft MVP - C++

      S 1 Reply Last reply
      0
      • S sgatto159

        Hi all, here's a question for you. What if one needs to do automatic disposing of a form's components when it is closed ? And what if one wan't write Dispose() for each component ? Here is my answer, I hope you agree is correct. I tested it in win2k/winXp with taskmanager and I have seen this piece of code effectively release the memory when the form is closed. Suggestions are really appreciated./* Here f is the Form. This method is to be called in the Dispose method of the subclass of your Form. */ public static void disposeComponents(Component f) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Component) { ((Component)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }

        S Offline
        S Offline
        sgatto159
        wrote on last edited by
        #3

        Second version: public static void disposeComponents(Component f, bool disposeForms) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Form && disposeForms == false) continue; if(value is Form && f is Form) { if( ((Form)f).ParentForm == value ) continue; if( ((Form)f).MdiParent == value ) continue; } if(value is IDisposable && value != null) { ((IDisposable)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }

        C 1 Reply Last reply
        0
        • C Christian Graus

          zu78 wrote: System.GC.Collect(); Read this[^]. In a nutshell, if you don't trust the Garbage Collector, use C++ instead ( my words, not his ). Christian Graus - Microsoft MVP - C++

          S Offline
          S Offline
          sgatto159
          wrote on last edited by
          #4

          I will read that (I' quite new to C#), but I don't like C++ ;)

          1 Reply Last reply
          0
          • S sgatto159

            Second version: public static void disposeComponents(Component f, bool disposeForms) { foreach (FieldInfo fieldInfo in f.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { object value = fieldInfo.GetValue(f); if(value is Form && disposeForms == false) continue; if(value is Form && f is Form) { if( ((Form)f).ParentForm == value ) continue; if( ((Form)f).MdiParent == value ) continue; } if(value is IDisposable && value != null) { ((IDisposable)value).Dispose(); fieldInfo.SetValue(f, null); } } System.GC.Collect(); }

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            I reiterate - if you don't trust the garbage collector, don't use it. You're fighting the framework, and performance can only suffer. Christian Graus - Microsoft MVP - C++

            S 1 Reply Last reply
            0
            • C Christian Graus

              I reiterate - if you don't trust the garbage collector, don't use it. You're fighting the framework, and performance can only suffer. Christian Graus - Microsoft MVP - C++

              S Offline
              S Offline
              sgatto159
              wrote on last edited by
              #6

              Forget the garbage collector (comment that line). I've posted that snippet to share with the community this idiom of disposing components in C# Windows Forms applications. Hope this clarify the topic of this post. Thanks however for the suggestion (as I have already said, I am new to C#)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups