Results 1 to 9 of 9

Thread: End vs. Unloading Forms

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Hi all.

    When ending a VB program, is it best to unload all of the program's Forms? Or is using the End statement enough?

    The reason that I am asking this is my program hesitates closing for a few moments when I unload the Forms, but it closes immediately when I use the End statement. However, I thought that it was better to unload the Forms because it frees up the system's memory?

    Any thoughts or suggestions anyone?

    Thanks,

    ------------------
    OneSource
    The truth may be out there, but it's in here too!
    .

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post

    Microsoft seems to think you should always unload your forms as opposed to Ending the program for normal termination. From MSDN:

    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 offorms andclass modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

    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.
    ----------------------------------------

    Code:
    Dim nFormCollectionIndex As Integer
        
    'Unload any forms that may still be open
    For nFormCollectionIndex = (Forms.Count - 1) To 0 Step -1
        Unload Forms(nFormCollectionIndex)
    Next nFormCollectionIndex
    That's all I've got on the subject...


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Thanks for the info. Seaweed.

    ------------------
    OneSource
    The truth may be out there, but it's in here too!
    .

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Hi.

    I have a follow-up question on this topic. If M$ recommends that all Forms be unloaded when ending a program, why does my program end so slowly when I unload the Forms while all M$ programs end so quickly? I mean, my program takes longer to end than Word or Excel and it is definitely not bigger than they are.

    Thanks for any thoughts.

    Chris

  5. #5
    New Member
    Join Date
    Jan 2000
    Location
    Blackpool
    Posts
    14

    Post

    Due to the fact that VB has to release all created objects and instances of objects, its good memory management and wont freeze your PC. Especially important with 3 tier programming

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Thanks SteFlitII.

    ------------------
    OneSource
    The truth may be out there, but it's in here too!
    .

  7. #7
    Lively Member
    Join Date
    Sep 2001
    Location
    Western North Carolina
    Posts
    124
    OneSource,

    Something that I have found to be useful.

    I unload all my forms as directed by M$. But I then insert an END statement directly after the Unload Statement. This may be over kill, but it seems to help me end program execution faster.

    Turtle


  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    If you assume that the computer is just sitting there idling along during those pauses, it makes sense to add the End statement after unloading the forms (I did that once, but I think all those programs have been thrown away a long time ago). However, that seems like a somewhat improbable assumption to make. More likely, there are several components that have to be cleaned up once your forms are unloaded, and the program is exiting, but before it has exited. If you use End, you could leave those objects in memory, but have nothing using them.

  9. #9
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627
    A couple app's I wrote and terminated using just the END statement, ended up still running in the background. I always unload all forms, then stick an end as the last line in the unload event.

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