Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: VB Snippet - Proper way to Exit your Application

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    VB Snippet - Proper way to Exit your Application

    VB Code:
    1. Dim frm as Form
    2.  
    3. For Each frm in Forms
    4.  
    5. Unload frm
    6.  
    7. Set frm=Nothing
    8.  
    9. Next
    10.  
    11. End
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    Member
    Join Date
    Nov 2002
    Posts
    54
    _
    Last edited by Darkolord; Mar 8th, 2003 at 10:54 AM.

  3. #3
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442
    Who said he uses timers?
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  4. #4
    Member
    Join Date
    Nov 2002
    Posts
    54

    Thumbs up

    oooops i'm sorry i didn't see that "End" at the end...
    Last edited by Darkolord; Mar 8th, 2003 at 10:55 AM.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Have you tested this theory?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442

    Talking well

    Yeah, works perfect
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Well

    Originally posted by James Stanich
    Have you tested this theory?
    You could put:
    VB Code:
    1. app.taskvisible = false

    instead of:

    VB Code:
    1. END
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    if you use "End" you had better be sure that you close any resources that you open via API's or dll's...

  9. #9
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442

    Re: Re: Well

    Originally posted by Nightwalker83
    You could put:
    VB Code:
    1. app.taskvisible = false
    What does that have to do with closing ur application?
    It's just to hide ur app from the windows tasklist
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Re: Re: Well

    Originally posted by phrozeman
    What does that have to do with closing ur application?
    It's just to hide ur app from the windows tasklist
    Using:

    VB Code:
    1. Unload me

    or

    VB Code:
    1. unload f

    Would just unload the application but it would still show in the windows tasklist.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    true, but i would still recommend using End instead of taskvisible. end does more than just removing the app from tasklist, which once the forms are unloaded properly, is a good thing
    If I agree with you today, don't get used to it.

  12. #12
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    Err, that only happens if you terminate your application in a bad way, or your computer is majorly screwed up, because I've never needed it...

    You'd want app.taskvisible=false if it takes a while to unload and you don't want your average user to notice. However, it does nothing with respect to unloading your program.

  13. #13
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by snakeeyes1000
    true, but i would still recommend using End instead of taskvisible. end does more than just removing the app from tasklist, which once the forms are unloaded properly, is a good thing
    Using:

    VB Code:
    1. END

    Is always bad practise because your program would crash.

    You'd want app.taskvisible=false if it takes a while to unload and you don't want your average user to notice. However, it does nothing with respect to unloading your program.
    I know but I have always had my application still show in the task window after I have closed it using:

    VB Code:
    1. unload me 'Or whatever

    I just add:

    VB Code:
    1. app.taskvisible=false

    To make sure I don't get this problem after the program finishes.
    Last edited by Nightwalker83; Mar 12th, 2003 at 02:28 AM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  14. #14
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    The problem with using app.taskvisible = false is that it won't unload your application! It'll only make it seem like it has been unloaded.

    If it isn't unloading when you do unload me, then you need to make sure that no functions being called after "unload me" are referencing any forms or anything in or on forms.

    Go ahead and attach a simple project where after the "unload me" statement (and ALL the forms have to be unloaded, not hidden), it does not disappear from the taskbar.

  15. #15
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    The problem with using app.taskvisible = false is that it won't unload your application! It'll only make it seem like it has been unloaded.
    I meant use it like this:

    VB Code:
    1. Private Sub Command1_Click()
    2. App.TaskVisible = False
    3. Unload Me
    4. End Sub

    On some computers the application name stayed in the Task List after using:

    VB Code:
    1. unload me

    That why I added it in I am NOT say you have to use it though.
    Last edited by Nightwalker83; Apr 19th, 2003 at 07:18 PM.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  16. #16
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Yep, definately a good point Aaron.

    I would also recommend chucking a Form.Visible = false line as well, makes for a slightly smother affect when a program closes.
    Don't Rate my posts.

  17. #17
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206

    VB.Net version?

    Ok, so, how's the VB.Net version of James' code?

  18. #18
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    The reason you are needing to put app.taskvisible = false or End after a form unload is because your unload ISNT unloading and destroying the form, but leaving a reference out there that you didn't cleanup. Try putting a:

    Code:
    set formname = nothing
    where formname is the actual form name in the unload event of the form.

    Do not use
    Code:
    set me = nothing
    If you unload the form and destroy its reference (assuming you have done the same for any other object you have created) you will not need End OR app.taskvisible = false

  19. #19
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344
    Originally posted by VBGuy
    The reason you are needing to put app.taskvisible = false or End after a form unload is because your unload ISNT unloading and destroying the form, but leaving a reference out there that you didn't cleanup. Try putting a:

    Code:
    set formname = nothing
    where formname is the actual form name in the unload event of the form.

    Do not use
    Code:
    set me = nothing
    If you unload the form and destroy its reference (assuming you have done the same for any other object you have created) you will not need End OR app.taskvisible = false
    If you bothered to read the posts you would have noticed that I have already stated what you have said.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  20. #20
    Lively Member flog3941's Avatar
    Join Date
    Nov 2002
    Posts
    123
    So how come when I use this code in VB.net the "forms" part is underlined like an error?

  21. #21
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Because this code is meant for VB6?

  22. #22
    Lively Member flog3941's Avatar
    Join Date
    Nov 2002
    Posts
    123
    So whats the code for "The proper way to exit an application" for VB.net, if you please.

  23. #23
    Member Skill Jaan's Avatar
    Join Date
    Apr 2003
    Location
    Islamabad, Pakistan
    Posts
    42
    why can't we use simply:
    End
    Why all this. I have always used only End and have not found anything wrong with it!!

  24. #24
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    End doesn't clean up memory used by your program and such. One of the guys will give you the list of why its a bad idea.

  25. #25
    Member
    Join Date
    May 2003
    Location
    Wisconsin
    Posts
    57
    How about............


    ___________________________

    Private Sub Form_Unload()

    Set Me = Nothing

    End Sub
    ___________________________


    ???????

  26. #26
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by PaulyWally
    How about............


    ___________________________

    Private Sub Form_Unload()

    Set Me = Nothing

    End Sub
    ___________________________


    ???????
    Well, nothing, except for the fact that it won't compile.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  27. #27
    Member
    Join Date
    May 2003
    Location
    Wisconsin
    Posts
    57
    Sorrrrrrrrrrrryyyyyyy.........


    ___________________________

    Private Sub Form_Unload()

    Set frmFormName = Nothing

    End Sub
    ___________________________

  28. #28

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Wow. What a reponse for such a simple task....
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  29. #29
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Skill Jaan
    why can't we use simply:


    Why all this. I have always used only End and have not found anything wrong with it!!
    I hope you never get into subclassing.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  30. #30
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    Originally posted by Pc_Madness
    End doesn't clean up memory used by your program and such. One of the guys will give you the list of why its a bad idea.
    Are you sure about that?

    I mean any memory you open up yourself doing anything tricky - fair enough you should clean up afterwards, but surely VB6 does its own garbage collection for normal stuff like forms? Certainly I can't find anything in the VB samples or documentation about this (sure somebody will now prove me wrong) and it seems like an important thing to document if it's true.

    We have a large and complex application that we run all day on several PC's. A timer closes it with a simple END statement each night and the PC's stay on 24/7 for the backup tape to run. If what you say is true, I would expect to see some memory getting chewed up each day - but Task Manager does not show any memory leakage at all.

    Now I've set myself up as a target, somebody want to take a shot?
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  31. #31
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Here's a great reason to not use End...

    Run the attached code and click the button.

    Edit: Whoops. The code is located three posts down.
    Last edited by The Hobo; Jun 22nd, 2003 at 01:36 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  32. #32
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by The Hobo
    Here's a great reason to not use End...

    Run the attached code and click the button.
    Frightening...
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  33. #33
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Using End in the Form_[Query]Unload() is bad. Using it elsewhere in your application is even worse. Why?

    Slap a CommandButton on your form, name it cmdEnd, and run this code:

    VB Code:
    1. Private Sub cmdEnd_Click()
    2.   End
    3. End Sub
    4.  
    5. Private Sub Form_Unload(Cancel As Integer)
    6.   MsgBox "I was called!"
    7. End Sub

    What doesn't happen?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  34. #34
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Whoops. The attached code:
    Attached Files Attached Files
    My evil laugh has a squeak in it.

    kristopherwilson.com

  35. #35
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    Well yes. Obviously if you go outside of native VB with a dll call to deliberately allocate memory and don't free it, of course you will have a problem.

    But if you simply have a number of forms open and call End from a module, VB seems to clean up after itself.

    I can't see it's possible to write a universal 'catch all' exit procedure guaranteed to clean up everything. You just have to do it on a case by case basis. And I'm not convinced you need to set any open forms to Nothing before Ending, so long as the End statement is not inside one of them. Vb seems to do that automatically. At least that is what experimentation seems to show. Anyone got sample code to demonstrate to the contrary?
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  36. #36
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Easy way around that...

    Just make the call BEFORE you send End.

    VB Code:
    1. Private Sub cmdEnd_Click()
    2.     Form_Unload
    3. End Sub
    4.  
    5. Private Sub Form_Unload()
    6.     Unload Me
    7.     MsgBox "I was called"
    8.     End
    9. End Sub
    Last edited by hothead; Jun 23rd, 2003 at 09:18 AM.

  37. #37
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I think this has been beaten to death, wouldn't you say?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  38. #38
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    Originally posted by crptcblade
    I think this has been beaten to death, wouldn't you say?
    I agree. This threads nearly as long as your signature
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  39. #39
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by hothead
    Easy way around that...

    Just make the call BEFORE you send End.

    VB Code:
    1. Private Sub cmdEnd_Click()
    2.     Form_Unload
    3. End Sub
    4.  
    5. Private Sub Form_Unload()
    6.     Unload Me
    7.     MsgBox "I was called"
    8.     End
    9. End Sub
    Now that's just redundant if you ask me. If the Form_Unload() has been called, your application will end afterwards. So why call End?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  40. #40
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    That was supposed to be Form_QueryUnload.

    If you look on Planet Source Code, search for BFChat, and look under the same sub inside the Client portion (bfcc.vbp I think it is), you'll see an example of this in action.

    If the user tries to exit while it's trying to connect, the program displays a message box telling you to wait. It doesn't call End yet until either it's completely connected, or fails to connect.

    If the user tries to exit, and the winsock state is not trying to connect, it will just exit as usual. Then look in your Task Manager if you're running XP. You will find that it's not under the Applications or Progresses tabs anymore. It has freed up the memory used by the program, as it's supposed to do.

    Granted the dude's code's a bit messed up (it takes some doing to figure out what the variables he used are for), but I think this is a good example of how you can do what you tried to do in your End.vbp file.
    Last edited by hothead; Jun 23rd, 2003 at 05:15 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width