Form Disposal and Null Containers
-
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.
-
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.
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.”
-
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.”
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.
-
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.
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.”
-
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.”
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.
-
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.
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.
-
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.
[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.