Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unload all forms vs. End

Status
Not open for further replies.

jopaumier

Programmer
Mar 15, 2002
97
US
Thread 222-622742 suggested unloading all the forms as a way to exit a VB program and unload it from memory. Wouldn't just using END accomplish the same thing? Are there advantages or disadvantages for each?

Jim
 
Generally Unloading the forms would be a better method as it ensures the application closes in the order you expect it to, and all associated tidy up methods are called, such as _unload, _terminate etc.

The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed.
 
If you are concerned with memory,
why not check the proccesses in the task manager
if you have win 2000 or better,
or oanother application.
so if all the memory was freed in both cases,
then end is preferrable because it's more convinient.
 
Unloading the forms is preferred, as End is indeed very abrupt and will not clean up things like database connections.

golandsh-
You're assuming they're running on NT, 2K, or XP. Many people are still running on 95, 98, & ME, where stopping a process doesn't always clean everything up.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks to all. I thought that hmckillop's answer would be the case and I would like to not leave anything in memory. I've put myself in a minor pickle that I need to sort out.

I have an Exit menu item as well as the control (close) box (like all Windows apps are supposed to have!). Since I do not know how a user might want to end a program, I thought about unloading forms in both places (with an appropriate call to a sub to loop through all open forms and unload each one in turn). But then, for example, if the user clicks on Exit on a form, the unload event for that form would be processed, going right back into the unload sub. Since one form has been unloaded, I get a subscript out of bounds error. Should I be using the terminate event in some way? This is something I have to simply think through and maybe my looping was not right.

Jim
 
Look into the Forms collection - it will have one member for every loaded form.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Johnwm,

I used forms.count to get the number of forms loaded and then unloaded each in turn. When I made what I thought was the correct fix to shut down the program by unloading each form, I got the subscript out of bounds error. I just have to take a little more care, think it through, and as you seggest take a closer look at the forms collection to see how to do this correctly.

Thanks for your input,
Jim
 
Jim -
If you're using a For..Next with a counter variable, you'll run into problems if you're doing this:
Code:
For i = 0 to Forms.Count - 1
   Forms(i).Close
Next i
[code]
Instead, do this:
[code]
For i = 0 to Forms.Count - 1
   Forms(0).Close   ' Note difference
Next i
[code]
If you close with an index, by the time you've closed just over half of them, the collection will have re-indexed itself and you'll the error you described.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Chip,

That is exactly what I am doing. Thanks for the tip. I'll give it a shot and get back to you (maybe tomorrow - I've decided to tackle easier stuff like revising messages!! --then on to another project).

Jim
 
The nice thing about collections is the For Each In... construct

Dim f as Form
For Each f in Forms
....
....
Next

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
johnwm,

Thanks for the slap on the side of the head. I had forgotten the syntax for the For with a collection.

I haven't really gotten back to this question, but I am wondering about the following (I may have mentioned it above):

The program leads the user through several forms before creating a report. I leave the forms open since the user can go back one form at a time and the information is still there or start over at the beginning. The user can 'shut down' at any point along the way using the control box or the menu Exit. I want to be able to tidy up where necessary and unload all the loaded forms so the program is not still running (the user does not know it is still running since there are no forms visible, which is what happened when a colleague tested the program). So I thought each form should have its own Unload event. And here is where I am unlcear about the process. Lets say 5 forms are loaded and after creating a few reports, the user decides to quit while on form 4 by clicking on the control box. The Unload event fires for form 4 and unloads one of the other forms, which has its own Unload event that fires and starts to unload forms. It seems like a cascading situation that must lead to a problem/crash. Is there a solution that won't crash? Or is this the correct solution and VB and Windows will eventually shut down gracefully?

Should this be done as an MDI?

Thanks,
Jim
 
You could write a routine in a module somewhere that does all your tidying up (closing forms, closing external docs/apps, closing db connections etc) and call that routine from the Query_Unload event and using the Unload_Mode argument to determine exact action to take.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
johnwm,

I have done just that, but I also created an MDI. The process is not exactly linear because the user has the ability to go back one form, start from the beginning (without shutting down) and run and run and run this program changing any/all files, options to create reports. On reviewing this thread and my program, I realize that I omitted this detail. Unloading forms without deleting some needed information became a bit too tricky for my brain, and could well be imposssible. With the MDI (which I am just finishing up), I can do what I need to do to create the as many reports as I like and then shut down with the parent form.

Thanks for your assistance and all others who contributed suggestions.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top