Επεξεργασία

Κοινή χρήση μέσω


End Statement

Terminates execution immediately.

Syntax

End  

Remarks

You can place the End statement anywhere in a procedure to force the entire application to stop running. End closes any files opened with an Open statement and clears all the application's variables. The application closes as soon as there are no other programs holding references to its objects and none of its code is running.

Note

The End statement stops code execution abruptly, and does not invoke 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.

The Stop statement suspends execution, but unlike End, it does not close any files or clear any variables, unless it is encountered in a compiled executable (.exe) file.

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 and Exit Statement) 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.

Important

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.

When followed by an additional keyword, End <keyword> Statement delineates the end of the definition of the appropriate procedure or block. For example, End Function terminates the definition of a Function procedure.

Example

The following example uses the End statement to terminate code execution if the user requests it.

Sub Form_Load()
    Dim answer As MsgBoxResult
    answer = MsgBox("Do you want to quit now?", MsgBoxStyle.YesNo)
    If answer = MsgBoxResult.Yes Then
        MsgBox("Terminating program")
        End
    End If
End Sub

Smart Device Developer Notes

This statement is not supported.

See also