Me.close vs. End

C

cj

What is the difference between using Me.close vs. End to stop the
execution of a program?

I've been using me.close and putting code in the form.closing event to
make sure things are stopped before the program execution stops. This
has been working for me even when the startup is sub main perhaps
because my sub mail usually looks like

<STAThread()> Public Sub main(ByVal CmdArgs() As String)
Dim mainForm As New Form1
Application.Run(mainForm)
End Sub
 
L

Larry Lard

cj said:
What is the difference between using Me.close vs. End to stop the
execution of a program?

Even more so than in earlier incarnations of VB, you *should not use
End*. This is from the docs (my emphases):
The End statement stops code execution abruptly, **without invoking the
Dispose or Finalize method**, or any other Visual Basic code. Object
references held by other programs are invalidated. **If an End
statement is encountered within a Try or Catch block, control does not
pass to the corresponding Finally block.**


Because End terminates your application without attending to any
resources that might be open, **you should try to close down cleanly
before using **it. For example, **if your application has any forms
open, you should close them before control reaches the End statement.**

**You should use End sparingly, and only when you need to stop
immediately.** The normal ways to terminate a procedure (Return
Statement (Visual Basic) and Exit Statement (Visual Basic)) not only
close down the procedure cleanly but also give the calling code the
opportunity to close down cleanly. A console application, for example,
can simply Return from the Main procedure.

**Security Note
The End statement calls the Exit method of the Environment class in the
System namespace. Exit requires that you have UnmanagedCode permission.
If you do not, a SecurityException error occurs.**
I've been using me.close and putting code in the form.closing event to
make sure things are stopped before the program execution stops. This
has been working for me even when the startup is sub main perhaps
because my sub mail usually looks like

<STAThread()> Public Sub main(ByVal CmdArgs() As String)
Dim mainForm As New Form1
Application.Run(mainForm)
End Sub

What you are doing is the right way to do things.
 
C

Cor Ligthert [MVP]

cj,

Close is closing (let the program stop after all things done that are needed
for that)

End is killing (stop it at the place it is)

Cor
 
Z

zacks

Cor said:
cj,

Close is closing (let the program stop after all things done that are needed
for that)

But doesn't Close just close the current Form? And if the current form
is not the main form for the application, the closing that form will
not close the application. Don't you have to do a Close event in the
main form to close the application?
 
G

gene kelley

But doesn't Close just close the current Form? And if the current form
is not the main form for the application, the closing that form will
not close the application. Don't you have to do a Close event in the
main form to close the application?

Have a look at the Application.Exit method

Gene
 
C

Cor Ligthert [MVP]

Zacks,.

If the mainform is the one that has its sub main in it (standard) than it
has instanced all its subforms as objects and therefore close those as
well.

If you use an seperated Sub Main have than a look at the message from Gene
about application exit.

Cor
 
L

Linda Liu [MSFT]

Hi,

Thank you for posting.

When Form.Close() is called, the form is closed and all resources created
within the object are closed and the form is disposed. The call of
Form.Close() invokes the Form.Closing and Form.Closed events. If the form
you are closing is the startup form of your application, your application
ends.

You can place the End statement anywhere in a procedure to force the entire
application to stop running. The End statement stops code execution
abruptly, without invoking the Dispose or Finalize method and the
Form.Closed or Form.Closing event. If an End statement is encountered
within a Try or Catch block, control does not pass to the corresponding
Finally block.If you have validation code in either of these events that
must be executed, you should call the Form.Close method for each open form
individually before calling the End statement.

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top