Results 1 to 17 of 17

Thread: Proper way to close and Application?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    84
    Stupid question, but I'm use to using "Stop" from VBscript, and I noticed that was the same as a breakpoint in VB6 Studio.


  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Here's the best way to close the application (anything but the EVIL statement, End):
    Code:
    ' If you have just one form:
    Call Unload(Me)
    
    ' If you have multiple forms:
    Dim Form As Form
    
    For Each Form In Forms
        Call Unload(Form)
    Next

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    For the first time in my VB career I don't agree with Yonatan

    I think this is the proper way to unload your form:

    Code:
    Private Sub From_Unload(Cancel As Integer)
    Dim Frm As Form
    
    For Each Frm In Forms
        Unload Frm
        Set Frm As Noting
    Next Frm
       End 'this is ok in this case!
    End Sub

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Jop,

    Code:
    Set Frm As Nothing
    Should be:
    Code:
    Set Frm = Nothing
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  5. #5
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    ??

    For the first time I disagree with Jop


    For Each Frm In Forms
    Unload Frm
    Set Frm As Noting ??????????? --- = Nothing (Noting is the infinitive of the verb to Note...)
    Next Frm

    Sorry Jop!... I couldn't resist....

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Oh oh oh oh oh oh

    I should shut my big mouth
    Sorry Yonatan

    I knew it but was too lazy to pay attention

    I'm soooo embarassed

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Jop: I read your code sixty-nine times, and arrived at several conclusions.

    • Form_Unload instead of From_Unload.
    • Form is waaaaay cuter than Frm.
    • I think Call Unload(Frm) looks better than Unload Frm (well, I'm a Call fan, ignore me ).
    • End isn't ok in any case. It is evil. Evil. EVIL!!! It will haunt you in your nightmares!
      Besides, it would exit the program whether or not you put the End there.
    • "=" instead of "As" and "Nothing" instead of "Noting" in Set Frm = Nothing. You'll get over it.
      Anyway, Frm is just a "pointer" to the form, so Set Frm = Nothing wouldn't help here.

    M'kay?

  8. #8
    Guest
    It is good to set the form to nothing so it frees up any resources and use the Unload statement with it as well.

    That's very good Yonatan. So many mistakes! Glad you aren't a teacher .

    And lighten up on Jop, he had the idea.

    I'm not disagreeing with anyone.

    But here, use this code.
    I'm right, as usual, your all wrong!

    Code:
    Public Sub UnloadAllForms()
    Dim Form As Form
    For Each Form In Forms
    Unload Form
    Set Form = Nothing
    Next Form
    End Sub
    
    Usage
    
    UnloadAllForms

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Matthew, this code looks better...
    Code:
    Public Sub UnloadAllForms()
        Dim Form As Form
        For Each Form In Forms
            Unload Form
            Set Form = Nothing
        Next Form
    End Sub
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  10. #10
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Originally posted by Matthew Gates
    I'm not disagreeing with anyone.
    ...
    I'm right, as usual, your all wrong!
    Self-contradiction sucks.
    Anyways, as I said, only one instance of each form exists, until it is unloaded with Unload.
    So, Set Form = Nothing won't help with anything, and will only make it a few milliseconds slower.
    You agree that this code:
    Code:
    For Each Form In Forms
        Call Unload(Form)
    Next Form
    Is pretty much equivalent to:
    Code:
    Set Form = Form1
    Call Unload(Form) ' Now Form1 is invalid
    Set Form = Form2
    Call Unload(Form) ' Now Form2 is invalid
    Set Form = Form3
    Call Unload(Form) ' Now Form3 is invalid
    ' And so on... (Form1 and Form2 and Form3 were all in memory)
    So, how would this help?
    Code:
    Set Form = Form1
    Call Unload(Form) ' Now Form1 is invalid
    Set Form = Nothing ' Why? It is already 100% not in memory,
    ' and now Form points at something illegal and setting it
    ' to Nothing doesn't do anything.
    Set Form = Form2 ' VB says: "It was just Nothing! Jeez!"
    Call Unload(Form)
    Set Form = Nothing
    Set Form = Form3 ' "Make up your mind!"
    ' Etc.
    So, I just proved that I'm right 99% of the time, and besides, I don't care about the remaining 3%.

    Lighten up some more? I tried, but couldn't jam any more smilies into that post.

    Well, that was useless, but fun!
    Yeah, and by the way, indentation rocks.

  11. #11
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Originally posted by Yonatan So, I just proved that I'm right 99% of the time, and besides, I don't care about the remaining 3%. [/B]
    100 - 3 = 97
    Oetje
    oetje@home.nl
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  12. #12
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    oetje: It's because there are three kinds of people in the world. Those who can count and those who can't.

  13. #13
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    Poor

    Poor Hambone....!

    He just asked a question he thought was a stupid question..!

    Ha!!Ha!!

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Exclamation the remaining 3% Yonatan, is coming back to get you

    Because you ever wondered why End has remained intact upto vb6 even if it has such a bad reputation. I'm using it regulary, and i'm going to use it as often as i can (just kidding).
    Code:
    Public Declare Function GetTickCount Lib "kernel32" () As Long
    Private die As Long, temp As Class1
    
    Sub main()
        Set temp = New Class1
        Form1.Show
        DoEvents
        Do
           If Forms.Count Then
            DoEvents
           Else
            If die Then
             If GetTickCount > die Then End 'Exit Do
            Else
             die = GetTickCount + 1000
            End If
           End If
        Loop
    End Sub
    Now actually i'm sure you can change this a bit and solve it wihtout End, but that's not the point. The point is that i have a break in class1 terminate event, and
    1. It doesn't fire when End is used
    2. It fires when Exit Do is used.

    That means End don't unload properly, but if you exit the code the sub main, it will. If you on the other hand use unload statement to unload the object, the codes will be equal. That's why End has this bad reputation. Now what proves you can have use of End? Well it's simple, if you just unload everything correctly, it can be equally usefull as a exit do, or exit for in a loop. Might not be much use but when you get into complicated algoritms, it can.

    t h E E n d
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008

    Unloading Forms

    Hmmm.

    Set frm = Nothing - releases a reference
    Unload frm - Unloads the form and kills references

    Neither is foolproof since if you access the forms properties in the Unload Event it will maintain the Instance Data associated with the form - the form might disappear from view but you won't have cleaned up properly...

    Your approaches are all far too wimpish...

    Take the BRS approach (Big Red Switch) for those who remember the IBM PC (original). Turn the bugger off and you will DEFINITELY release that memory.

    So you might get a bit of data corruption - means they have to rely on some guru to fix it...

    Wonder who that might be?

    Hehehe.

    Cheers all,

    Paul
    Not nearly so tired now...

    Haven't been around much so be gentle...

  16. #16
    Lively Member
    Join Date
    Oct 2000
    Location
    Scotland
    Posts
    96
    Sub ExitForms()

    Dim iFormCount As integer

    For iFormCount = Forms.Count - 1 To 0 Step -1

    Unload Forms(iFormCount)

    Next iFormCount

    End

    Exit Sub

    This will also do - a little bit simple compared to the above but it works. It ensures all the forms are unload before closing the application.

    H.

  17. #17
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    400
    You do need to Set frm = Nothing to release a form completely from memory, according to the help file:

    "The only way to release all memory and resources is to unload the form and then set all references to Nothing. The reference most commonly overlooked when doing this is the hidden global variable mentioned earlier. If at any time you have referred to the form by its class name (as shown in the Properties Window by the Name property), you've used the hidden global variable. To free the form's memory, you must set this variable to Nothing."

    This quote is under "Life Cycle of Visual Basic Forms" in MSDN.

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