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. .NET (Core and Framework)
  4. Form Disposal and Null Containers

Form Disposal and Null Containers

Scheduled Pinned Locked Moved .NET (Core and Framework)
dockerregexjsonquestion
7 Posts 3 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.
  • T Offline
    T Offline
    Tristan Rhodes
    wrote on last edited by
    #1

    Hi Guys, I'm trying to get my forms to clean up after themselves when i dispose them. The designer builds all the lovely disposal pattern stuff, with the body: if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); Which is all well and good, but as i add components to the designer, i would assume they would be placed in a container and cleaned up when the form disposes, however, the container is always null, and hence the dispose is never called. Because of this my background worker keeps running and my underlying win32 api calls are never closed down. While i can (and will) do this the manual way, is there any way to have the designer wire up that logic for me? Regards Tris

    ------------------------------- Carrier Bags - 21st Century Tumbleweed.

    H 1 Reply Last reply
    0
    • T Tristan Rhodes

      Hi Guys, I'm trying to get my forms to clean up after themselves when i dispose them. The designer builds all the lovely disposal pattern stuff, with the body: if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); Which is all well and good, but as i add components to the designer, i would assume they would be placed in a container and cleaned up when the form disposes, however, the container is always null, and hence the dispose is never called. Because of this my background worker keeps running and my underlying win32 api calls are never closed down. While i can (and will) do this the manual way, is there any way to have the designer wire up that logic for me? Regards Tris

      ------------------------------- Carrier Bags - 21st Century Tumbleweed.

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      If this is a Windows Forms application and you are adding the components using the designer the necessary code for disposing them is generated automatically, or at least should be. As far as a background worker is concerned, it is working in the background, in a different thread, and that will continue to run. Try handling the FormClosing event and stop the background worker there.

      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

      T 1 Reply Last reply
      0
      • H Henry Minute

        If this is a Windows Forms application and you are adding the components using the designer the necessary code for disposing them is generated automatically, or at least should be. As far as a background worker is concerned, it is working in the background, in a different thread, and that will continue to run. Try handling the FormClosing event and stop the background worker there.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        T Offline
        T Offline
        Tristan Rhodes
        wrote on last edited by
        #3

        Henry Minute wrote:

        or at least should be.

        Yeah, i thought the same, but it's clearly not doing any finalizing or disposing when i close the form. The app is quite complicated, a lot of components being shared between multiple forms, but some of the forms contain components only applicable in that scope, and the form gets re-used, so i don't want to do cleanup on close, but only on dispose which occurs when the app gets dismantled.

        ------------------------------- Carrier Bags - 21st Century Tumbleweed.

        H 1 Reply Last reply
        0
        • T Tristan Rhodes

          Henry Minute wrote:

          or at least should be.

          Yeah, i thought the same, but it's clearly not doing any finalizing or disposing when i close the form. The app is quite complicated, a lot of components being shared between multiple forms, but some of the forms contain components only applicable in that scope, and the form gets re-used, so i don't want to do cleanup on close, but only on dispose which occurs when the app gets dismantled.

          ------------------------------- Carrier Bags - 21st Century Tumbleweed.

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          Ah. I'm not sure of what happens in Dispose() when the control/component is referenced elsewhere. You could try handling Application.ApplicationExit maybe?

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          T 1 Reply Last reply
          0
          • H Henry Minute

            Ah. I'm not sure of what happens in Dispose() when the control/component is referenced elsewhere. You could try handling Application.ApplicationExit maybe?

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            T Offline
            T Offline
            Tristan Rhodes
            wrote on last edited by
            #5

            I've got the Close event handled in the application host form, and it disposes the primary application container, but i was hoping it would tidy up the various components in the nested containers / forms. (And thinking about this gives me a headache...)

            ------------------------------- Carrier Bags - 21st Century Tumbleweed.

            P 1 Reply Last reply
            0
            • T Tristan Rhodes

              I've got the Close event handled in the application host form, and it disposes the primary application container, but i was hoping it would tidy up the various components in the nested containers / forms. (And thinking about this gives me a headache...)

              ------------------------------- Carrier Bags - 21st Century Tumbleweed.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Sounds like you need to consider using a WeakReference or two.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              T 1 Reply Last reply
              0
              • P Pete OHanlon

                Sounds like you need to consider using a WeakReference or two.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                T Offline
                T Offline
                Tristan Rhodes
                wrote on last edited by
                #7

                [Edit] - Never mind, i didn't realize that WeakReference was a class. I thought it was just the opposite of tight coupling. :P Now... coffee...

                ------------------------------- Carrier Bags - 21st Century Tumbleweed.

                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