"End Task" from Windows Task List Invokes No VB Unload Event

ID Number: Q80292

1.00

WINDOWS

Summary:

Terminating a program written in Visual Basic from the Windows Task

List (by choosing the End Task button) does not generate a Visual

Basic Unload event. Any code that is attached to the Form_Unload

event procedure will not be executed and the program will terminate.

This behavior is by design.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

If a Visual Basic program has code in an Form_Unload event procedure,

and you exit the program by choosing Close from the system menu, the

Unload event occurs and the code in the Form_Unload event procedure

will execute. If instead you exit the program from the Windows Task

List, the Unload event never occurs and the code in the Form_Unload

event procedure is never executed. This behavior is demonstrated in

the example code below.

Below are the steps necessary to make a Visual Basic code example.

1. Run Visual Basic or from the File menu, choose New Project (ALT, F,

N) if Visual Basic is already running. Form1 will be created by

default.

2. Place a command button (Command1) on Form1:

3. Add the following code to the Command1_Click event procedure for

Command1:

Sub Command1_Click()

Unload Form1

End Sub

4. Add the following code to the Form1_Unload event procedure:

Sub Form1_Unload(Cancel as integer)

If MsgBox("Continue to unload the form?",1) = 2 Then

cancel = -1

Else

cancel = 0

End If

End Sub

5. From the File menu, choose Make EXE File (ALT, F, K). Enter a

filename for the .EXE program and choose the OK button.

6. From the Windows Program Manager File menu, choose Run (ALT, F, R),

and enter the name of the .EXE file created in step 5 above.

While the program is running, exit either by double-clicking the

system menu or by pressing the command button. You will see the

message box appear and you will be able to abort unloading the form if

you choose the Cancel button.

Now try running the program (step 6 above) and bring up the Windows

Task List by pressing CTRL+ESC. If you select the name of the program

from the Task List and then choose the End Task button, your program

will terminate without the message box ever being displayed.

Additional reference words: 1.00