I just finished building my application that I built in VB6. I have an Exit button on the main form that unloads the main form. I have Unload me code already. What is the line of code that ends the running of my code?
Printable View
I just finished building my application that I built in VB6. I have an Exit button on the main form that unloads the main form. I have Unload me code already. What is the line of code that ends the running of my code?
To unload the form you are currently working with, you would use
Unload Me
and generally people like to use this code in the form's unload event to clean up after multiple forms...
VB Code:
Dim frm As Form For Each frm In Forms Unload frm Set frm = Nothing Next End
:)
if u mean unload the form just use
Unload Me
I have done that already. But VB6 still stays in run mode. So what else do I need to end my run mode?
Unload Me
End
usually ppl dont use End, but just try it after the unload me :rolleyes:
Read above and you will see the answer has been already posted...Quote:
Originally posted by Avatarp
I have done that already. But VB6 still stays in run mode. So what else do I need to end my run mode?
VB Code:
Dim frm As Form For Each frm In Forms Unload frm Set frm = Nothing Next End
End works...LOL that was easy
ENDQuote:
Originally posted by Avatarp
End works...LOL that was easy
This is the most unprofessional, unreliable way of exiting a program...
i told him to try Unload Me first, but didnt seem to work for him so i said End :rolleyes:
So now we know somethin in the program isn't ending properly... an unreleased resource...
Huh ? :confused:Quote:
Originally posted by nemaroller
So now we know somethin in the program isn't ending properly... an unreleased resource...
i think hes saying, since Unload Me isnt working... part of the program is still running .....right?:confused:
yes
And using this code doesn't work for you?
VB Code:
Dim frm As Form For Each frm In Forms Unload frm Set frm = Nothing Next End
This is how I end all my apps...
If your have more than 1 form still loaded in your project, then you need to unload that as well.
The code I posted will unload all the forms !!!
if his app still wasnt ending And he placed teh code in the right place, something was still running :p
just not sure what tho:rolleyes:
Yeah.. or you could just doQuote:
Originally posted by James Stanich
The code I posted will unload all the forms !!!
Unload Form1
Unload Form2.
Alot less code if he only has 2 forms.
If this code is place in any form in the project, all forms and form levels variables will be destroyed... this would in turn release all memory being consumed, and close the application... I do not understand the argument here...
James is correct. His code will unload all forms and clear up the memory they take, but if there are any loops running, it won't stop them. You will need to stop the loop before you can exit the program and release all the resources it's taking.
Loops running at time of close? You mean a crash in the loop?
No, I mean if you put in a loop somewhere that is running, say you put this on a form, then try to exit the program using your code, it will close the form, but it will still be in memory:
VB Code:
For I = 0 to 500000000 Debug.Print I DoEvents Next
But wouldn't "i" be terminated, thus terminating the loop?
Nope. Try it. I just did, and it just keeps printing to the debug window, although the app has closed.
That's amazing. The varaibles should be destroyed but are not. Scary thought....:eek:Quote:
Originally posted by MidgetsBro
Nope. Try it. I just did, and it just keeps printing to the debug window, although the app has closed.
Because you are just setting the forms to nothing, you aren't setting any variables to nothing. You would have to loop through all your variables you declared earlier in the program and set each one to nothing, if that's even possible.
By setting the form = nothing, I was lead to belive that the form level variables were alos destroyed. Was I mislead? :confused:Quote:
Originally posted by MidgetsBro
Because you are just setting the forms to nothing, you aren't setting any variables to nothing. You would have to loop through all your variables you declared earlier in the program and set each one to nothing, if that's even possible.
well what would be so bad about using End in this situation?
or couldt it possibly cause an error:confused:
Either that, or the programmers of VB intended for that to happen, but then they never implemented it into the language...
The best way to do this is to check a "Quitting" variable in each loop that might run for any length of time that is user noticable, an if it's set to true in the form_QueryUnload event, then in the loops you can exit the loop if the variable is set to true.
VB Code:
'General Declares Dim Quitting As Boolean Dim LoopQuitSuccessful As Boolean Private Sub EndlessLoop() Do Debug.Print "Doing Nothing" If Quitting Then LoopQuitSuccessful = True Exit Loop End If DoEvents Loop End Sub Private Sub Form_QueryUnload(Cancel As Integer) Quitting = True Do Until LoopQuitSuccessful DoEvents Loop End Sub
End won't cause an error, but it doesn't free up memory either. It just quits the process, but leaves everything it used, instead of cleaning it up. If the user ends the program like this numerous times in a single session (like if they don't shut their computer down often) then they will run out of resources and be forced to shut down or restart the computer.
Since we are beating this to death here, what about global variables, is there a proper way of releasing that memory?
Not sure about globals, as I don't use them often, but if you don't set them to nothing, then I assume they will stay in memory as well... *ponders*
oh ok lol
end.Bad = True :D
At least someone learned something in all this babble...:DQuote:
Originally posted by ice_531
oh ok lol
end.Bad = True :D
lol :D
i learn something new every day on this forum ;)
I learnt something as well. Don't listen to myself. :DQuote:
Originally posted by James Stanich
At least someone learned something in all this babble...:D
DAMMIT....Quote:
Originally posted by MidgetsBro
End won't cause an error, but it doesn't free up memory either. It just quits the process, but leaves everything it used, instead of cleaning it up. If the user ends the program like this numerous times in a single session (like if they don't shut their computer down often) then they will run out of resources and be forced to shut down or restart the computer.
No wonder my App keeps freezing me up..
THANKS DOOD!
Also,
i put the code above (for each frm) in my "Unload" statement?
What i usually do is create a global sub in a module which when I want to exit any form I call like this
VB Code:
Call GOODEXIT
Then the subroutine
VB Code:
Sub GOODEXIT() Dim frm As Form For Each frm In Forms Unload frm Set frm = Nothing Next End End Sub
dont u need
at the top, andVB Code:
Public
VB Code:
End Sub
at the bottom?
Well, I put this in a module, call it from a command button, with the next line in Command_click being to unload the form and set it to nothing, which it does even though the loop continues.Quote:
For I = 0 to 500000000
Debug.Print I
DoEvents
Next
But, as soon as the loop finishes... the program ends properly.
yes sir. Sorry... I edited my post...:)Quote:
Originally posted by wpearsall
dont u need
at the top, andVB Code:
Public
VB Code:
End Sub
at the bottom?
OK,,
Unloading all the forms like dat, does it unload modules? Or do u have to do a diff code for those (wanna free as much resources as possible.. since my app restarts like 4 / 5 times in a typical "session" :D
This has yet to be determined.... :(Quote:
Originally posted by MidgetsBro
Not sure about globals, as I don't use them often, but if you don't set them to nothing, then I assume they will stay in memory as well... *ponders*
Visual Basic Help:
The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.
well, my program is closing with the code
for each frm in Forms
and i left the End off of it, to test it, so i guess it closes "In Active" modules.
Not that it would matter, but wouldn't this method leave the return address of the calling procedure on the stack for a few picoseconds before the program's stack space is reclaimed?Quote:
Originally posted by James Stanich
VB Code:
Call GOODEXIT Sub GOODEXIT() . . . End End Sub
:p
Thanks for the replys on my post. I used the end statement to terminate my application and it works fine. But I didn't realize the amount of resources that get used up if you don't clear all variable used. So in the future I use that good exit sub routine to end my applications.
Thanks again
Damn, that deep....:pQuote:
Originally posted by Mortivan
Not that it would matter, but wouldn't this method leave the return address of the calling procedure on the stack for a few picoseconds before the program's stack space is reclaimed?
:p
here is what you do if you have a button
Private Sub Command1_Click()
Unload Me
End Sub
http://www.vbforums.com/attachment.p...postid=1213821
make a button, name it Command 1 and put this in the buttons code:
Private Sub Command1_Click()
Unload Me
End Sub
when exiting a program I do the following
since an active timer can trigger a form to re-appear...
VB Code:
Dim Form As Form, timerx As Object 'close all sub forms For Each Form In Forms For Each timerx In Form If TypeOf timerx Is Timer Then timerx.Enabled = False End If Next Unload Form Next Form
I also wonder about how to 'unreference' ALL references in each form (ie set blaa = Nothing) in case they are still using memory.
or maybe this is done automatically on Unload Form?
/Me is watching this closly :D
The code above works WELL BETTER since my "Mega Usage" Application (What i open over and over in a session) uses timers, and they trigger re-opening on errors ETC :D
:Better Idea's (if any) Will be tested ;)