Closed form keeps executing

PJRoyle

Member
Joined
Jun 7, 2004
Messages
11
Location
Uckfield, East Sussex, UK
Programming Experience
10+
I have a form which carries out some tasks in response to a timer. Under certain circumstances, after closing the form, the timer continues to run and the tasks it triggers are still being carried out. The Help says that if a form is closed, all resources related to it are disposed of - so how can code in a closed form keep executing??

TIA (I hope)
Peter Royle
 
Closed form not closed

I'll try to answer this but keep in mind, you left a few details out. So, if this doesn't explain it please repost with more details.

Memory is allocated when the code "dim x as new...." is encountered. "X" is a pointer variable to the beginning memory where the form is stored. MS documentation leads you to believe that the memory is wiped when the code "x = nothing" is encountered. Nothing could be futher from the truth. What happens is that memory location of "X" is released back to the stack making that memory location available for other uses. The memory allocated for the form is still taken up with the form's value(s). So, when the trigger event is fired vb.net attempts to find "X" and guess what? Even tho its been released its still there because no other use has been allocated for memory location known as "X"! so, your form appears to be still resident. Be warned tho, if you program your code assuming this is always the case another dim may be encountered that may take the memory location for "X" and poof - your form no longer can be accessed because now the memory location of "X" is being used for something else.

so, get it? how do you fix your problem? don't know, not there, don't want to see it. but, knowing what i've written above may help you recode your processing so that this situation doesn't affect your processing.

ed.

Isn't coding with MS products fun!
 
in the forms closing event (or closed event) simply disable the timer control timer1.enabled = false that'll keep it from fireing after the form's been disposed of
 
Ed,

thanks, that's really helpful - not!! But it's not your fault. Do you know the old Chinese curse, "may you live in interesting times"? I think it should be re-worked to "may you work in an interesting development environment" .

Cheers, anyway,
Peter
 
Another reply to Ed

Ed,

someone on the microsoft.dotnet usergroup thinks that setting the variable used to hold the form (when it does "dim frm as new form1") to Nothing will do the trick. Does this free up the memory we have gobbled up? And get round the potential errors you described?

Peter Royle
 
maybe, maybe not. VB.NET does its own garbage collection which means, you have to rely on Microsoft (VB.NET) to clean up your disk space. In C++ its up to the programmer to accomplish garbage collection. VB.NET does a pretty good job in performing garbage collection. I've written some fairly complex programs that instantiate some in-house, 3rd party and Microsoft supplied objects in VB.NET and haven't run in to signs of memory problems so far. As I mentioned, simply setting an object to nothing doesn't necessarily guarentee that the memory will be deallocated right away. You can test it easily enough. Create a Windows service. In the service simply create objects and then set them to nothing in some infinite loop construct. While the service is running bring up task manager and click on the performance tab and watch how your system is reacting to gobbling up memory.Ed.
 
EdAlaska said:
maybe, maybe not. VB.NET does its own garbage collection which means, you have to rely on Microsoft (VB.NET) to clean up your disk space. In C++ its up to the programmer to accomplish garbage collection. VB.NET does a pretty good job in performing garbage collection. I've written some fairly complex programs that instantiate some in-house, 3rd party and Microsoft supplied objects in VB.NET and haven't run in to signs of memory problems so far. As I mentioned, simply setting an object to nothing doesn't necessarily guarentee that the memory will be deallocated right away. You can test it easily enough. Create a Windows service. In the service simply create objects and then set them to nothing in some infinite loop construct. While the service is running bring up task manager and click on the performance tab and watch how your system is reacting to gobbling up memory.Ed.

C++.net (and C#.net and all dat) rely on the .net framework (windows) to do garbage collection, all .net apps shouldnt do any garbage collection whatsoever, leave it to the system.

pj... like i said in the form's closed event set the timer's enabled option to False and that'll stop the timer from triggering any events while the form's closed... problem solved
 
Back
Top