Making sure a form unloads before a program exits 
Author Message
 Making sure a form unloads before a program exits

Is there a standard way to make sure that a form unloads
completely before a program ends?

I have a global function EndProgram which looks like this:

Public Sub EndProgram()
    ' Do cleanup routines
    ' set global objects to be nothing (not forms)
    If Not Form1 Is Nothing Then
        Unload Form1
    End If
    If Not Form2 Is Nothing Then
        Unload Form2
    End If
    Set Form1 = Nothing
    Set Form2 = Nothing
    End
End Sub

Here are my concerns / questions:

1.  I'm feeling a bit bad about calling the
function "End", but I'm finding it very difficult to make
sure that every part of every form is properly destroyed.  
Are there strategies to make that process more transparent?

2.  Is there a better way to force a form to unload?

Thanks,
Brian



Sun, 13 Feb 2005 03:53:25 GMT  
 Making sure a form unloads before a program exits
If you destroy all the objects and unload all your forms you won't have to
use the "Evil End".

Try this :

        Private Sub Form_Unload(Cancel As Integer)

            Dim frm As Form

            For Each frm In Forms
                Unload frm
            Next frm

            Set frm = Nothing

        End Sub

if everything has been destroyed properly your program should stop correctly
;O)

--
Best Regards
Zoury
_________________________________
Thanks to post replies to the newsgroup :O)

Quote:
> Is there a standard way to make sure that a form unloads
> completely before a program ends?

> I have a global function EndProgram which looks like this:

> Public Sub EndProgram()
>     ' Do cleanup routines
>     ' set global objects to be nothing (not forms)
>     If Not Form1 Is Nothing Then
>         Unload Form1
>     End If
>     If Not Form2 Is Nothing Then
>         Unload Form2
>     End If
>     Set Form1 = Nothing
>     Set Form2 = Nothing
>     End
> End Sub

> Here are my concerns / questions:

> 1.  I'm feeling a bit bad about calling the
> function "End", but I'm finding it very difficult to make
> sure that every part of every form is properly destroyed.
> Are there strategies to make that process more transparent?

> 2.  Is there a better way to force a form to unload?

> Thanks,
> Brian



Sun, 13 Feb 2005 04:02:08 GMT  
 Making sure a form unloads before a program exits

Thanks, Zoury,
Brian



Sun, 13 Feb 2005 04:32:56 GMT  
 Making sure a form unloads before a program exits
Hi,

Personally I use the "Set <form> = Nothing" statement in appropriate places
for any Forms that I load.
Otherwise not all resources may be freed by just closing the Form, causing
your application to have a larger footprint than is necessary.
Although it is fine for smaller applications, for large applications with
many dialogs, waiting until the "main" Form's Unload Event is fired doesn't
make the best use of resources.

David
lilchips.com


Quote:
> If you destroy all the objects and unload all your forms you won't have to
> use the "Evil End".

> Try this :

>         Private Sub Form_Unload(Cancel As Integer)

>             Dim frm As Form

>             For Each frm In Forms
>                 Unload frm
>             Next frm

>             Set frm = Nothing

>         End Sub

> if everything has been destroyed properly your program should stop
correctly
> ;O)

> --
> Best Regards
> Zoury
> _________________________________
> Thanks to post replies to the newsgroup :O)


> > Is there a standard way to make sure that a form unloads
> > completely before a program ends?

> > I have a global function EndProgram which looks like this:

> > Public Sub EndProgram()
> >     ' Do cleanup routines
> >     ' set global objects to be nothing (not forms)
> >     If Not Form1 Is Nothing Then
> >         Unload Form1
> >     End If
> >     If Not Form2 Is Nothing Then
> >         Unload Form2
> >     End If
> >     Set Form1 = Nothing
> >     Set Form2 = Nothing
> >     End
> > End Sub

> > Here are my concerns / questions:

> > 1.  I'm feeling a bit bad about calling the
> > function "End", but I'm finding it very difficult to make
> > sure that every part of every form is properly destroyed.
> > Are there strategies to make that process more transparent?

> > 2.  Is there a better way to force a form to unload?

> > Thanks,
> > Brian



Sun, 13 Feb 2005 06:03:48 GMT  
 Making sure a form unloads before a program exits
Hi David!

Quote:
> Personally I use the "Set <form> = Nothing" statement in appropriate
places
> for any Forms that I load.

    I do Unload all the forms right after their use. I use the code showed
in the other post to make sure I haven't forgot any forms or reload one by
mistake.

Quote:
> Otherwise not all resources may be freed by just closing the Form, causing
> your application to have a larger footprint than is necessary.

    I thought, correct me if I'm wrong, that Set Form1 = Nothing and Unload
Form1 were the same. I don't think that Unload Me just close the form... it
unload it...

Quote:
> Although it is fine for smaller applications, for large applications with
> many dialogs, waiting until the "main" Form's Unload Event is fired
doesn't
> make the best use of resources.

    I agreed totaly! ;O)

--
Best Regards
Zoury
_________________________________
Thanks to post replies to the newsgroup :O)

Quote:
> Hi,

> Personally I use the "Set <form> = Nothing" statement in appropriate
places
> for any Forms that I load.
> Otherwise not all resources may be freed by just closing the Form, causing
> your application to have a larger footprint than is necessary.
> Although it is fine for smaller applications, for large applications with
> many dialogs, waiting until the "main" Form's Unload Event is fired
doesn't
> make the best use of resources.

> David
> lilchips.com



> > If you destroy all the objects and unload all your forms you won't have
to
> > use the "Evil End".

> > Try this :

> >         Private Sub Form_Unload(Cancel As Integer)

> >             Dim frm As Form

> >             For Each frm In Forms
> >                 Unload frm
> >             Next frm

> >             Set frm = Nothing

> >         End Sub

> > if everything has been destroyed properly your program should stop
> correctly
> > ;O)

> > --
> > Best Regards
> > Zoury
> > _________________________________
> > Thanks to post replies to the newsgroup :O)


> > > Is there a standard way to make sure that a form unloads
> > > completely before a program ends?

> > > I have a global function EndProgram which looks like this:

> > > Public Sub EndProgram()
> > >     ' Do cleanup routines
> > >     ' set global objects to be nothing (not forms)
> > >     If Not Form1 Is Nothing Then
> > >         Unload Form1
> > >     End If
> > >     If Not Form2 Is Nothing Then
> > >         Unload Form2
> > >     End If
> > >     Set Form1 = Nothing
> > >     Set Form2 = Nothing
> > >     End
> > > End Sub

> > > Here are my concerns / questions:

> > > 1.  I'm feeling a bit bad about calling the
> > > function "End", but I'm finding it very difficult to make
> > > sure that every part of every form is properly destroyed.
> > > Are there strategies to make that process more transparent?

> > > 2.  Is there a better way to force a form to unload?

> > > Thanks,
> > > Brian



Sun, 13 Feb 2005 20:36:55 GMT  
 Making sure a form unloads before a program exits
Hi,

Quote:
> I thought, correct me if I'm wrong, that Set Form1 = Nothing and Unload
> Form1 were the same. I don't think that Unload Me just close the form...
it
> unload it...

Unload does unload the GUI portion of the Form, but depending on what is in
the class module, it may not free all of that.  In other words, if you have
public or static variables etc. in the Form, simply calling Unload won't
free up the resources required by the module.

Bob Butler posted a good explanation of this over in
microsoft.public.vb.general.discussion.
See the message called: a question about static variable

To quote part of his message:

Forms are divided into two pieces.  The first part is essentially a class
module and has the code, module-level variables and local static variables.
The second part is the GUI with the controls and other visual elements.

When you click the X you are unloading the GUI part but not necessarily the
class part.  All control properties will be reset to defaults if the form is
reloaded but unless the reference to the class part is released the
variables stay.

David
lilchips.com


Quote:
> Hi David!

> > Personally I use the "Set <form> = Nothing" statement in appropriate
> places
> > for any Forms that I load.

>     I do Unload all the forms right after their use. I use the code showed
> in the other post to make sure I haven't forgot any forms or reload one by
> mistake.

> > Otherwise not all resources may be freed by just closing the Form,
causing
> > your application to have a larger footprint than is necessary.

>     I thought, correct me if I'm wrong, that Set Form1 = Nothing and
Unload
> Form1 were the same. I don't think that Unload Me just close the form...
it
> unload it...

> > Although it is fine for smaller applications, for large applications
with
> > many dialogs, waiting until the "main" Form's Unload Event is fired
> doesn't
> > make the best use of resources.

>     I agreed totaly! ;O)

> --
> Best Regards
> Zoury
> _________________________________
> Thanks to post replies to the newsgroup :O)


> > Hi,

> > Personally I use the "Set <form> = Nothing" statement in appropriate
> places
> > for any Forms that I load.
> > Otherwise not all resources may be freed by just closing the Form,
causing
> > your application to have a larger footprint than is necessary.
> > Although it is fine for smaller applications, for large applications
with
> > many dialogs, waiting until the "main" Form's Unload Event is fired
> doesn't
> > make the best use of resources.

> > David
> > lilchips.com



> > > If you destroy all the objects and unload all your forms you won't
have
> to
> > > use the "Evil End".

> > > Try this :

> > >         Private Sub Form_Unload(Cancel As Integer)

> > >             Dim frm As Form

> > >             For Each frm In Forms
> > >                 Unload frm
> > >             Next frm

> > >             Set frm = Nothing

> > >         End Sub

> > > if everything has been destroyed properly your program should stop
> > correctly
> > > ;O)

> > > --
> > > Best Regards
> > > Zoury
> > > _________________________________
> > > Thanks to post replies to the newsgroup :O)


> > > > Is there a standard way to make sure that a form unloads
> > > > completely before a program ends?

> > > > I have a global function EndProgram which looks like this:

> > > > Public Sub EndProgram()
> > > >     ' Do cleanup routines
> > > >     ' set global objects to be nothing (not forms)
> > > >     If Not Form1 Is Nothing Then
> > > >         Unload Form1
> > > >     End If
> > > >     If Not Form2 Is Nothing Then
> > > >         Unload Form2
> > > >     End If
> > > >     Set Form1 = Nothing
> > > >     Set Form2 = Nothing
> > > >     End
> > > > End Sub

> > > > Here are my concerns / questions:

> > > > 1.  I'm feeling a bit bad about calling the
> > > > function "End", but I'm finding it very difficult to make
> > > > sure that every part of every form is properly destroyed.
> > > > Are there strategies to make that process more transparent?

> > > > 2.  Is there a better way to force a form to unload?

> > > > Thanks,
> > > > Brian



Mon, 14 Feb 2005 02:34:47 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Making sure VB program leaves memory when done.

2. Problem getting program to end after unloading the form (Unload Me)

3. Making Sure a Form is Displayed

4. Unload (best method to exit a program)

5. MDI Help - Prevent Child Form from Exiting in Unload Event

6. MDI Help - Prevent Child Form from Exiting in Unload Event

7. how to unload all forms upon exit

8. Unloading modal owner form also unloads modal form?

9. Making sure word isn't running

10. Making sure XML DLL is installed correctly?

11. Can VB return exit code on program termination as exit(X) in C

12. Q: Making sure disconnected RS is used?

 

 
Powered by phpBB® Forum Software