PRB: Terminate Events Fire in Reverse Order in VBCE/eVB (250477)
The information in this article applies to:
- Microsoft Windows CE Toolkit for Visual Basic 6.0
- Microsoft eMbedded Visual Basic 3.0
This article was previously published under Q250477 SYMPTOMS
When two forms are shown and closed, the Terminate event of the second form fires prior to the Terminate event of the first form. The order of these events is opposite of what occurs in Microsoft Visual Basic 6.0. This behavior occurs in the emulation environment and on the remote device.
RESOLUTION
In cases where this behavior needs to be worked around, project level variables can be used to store when forms in the project are loaded and unloaded. Each form in the project can call a generic Terminate routine. When the generic Terminate event is called and all forms that had been loaded have been unloaded, the Terminate code can be executed.
The following example demonstrates this concept:
- Create a new Windows CE project in either Visual Basic or eMbedded Visual Basic. Form1 is created by default.
- Paste the following code into Form1:
Private Sub Form_Click()
Form2.Show
End Sub
Private Sub Form_Load()
Form1Unloaded = False
End Sub
Private Sub Form_Terminate()
TerminateHandler
End Sub
Private Sub Form_Unload(Cancel As Integer)
Form1Unloaded = True
End Sub
- Add a second form to the project.
- Paste the following code into Form2:
Private Sub Form_Load()
Form2Unloaded = False
End Sub
Private Sub Form_Terminate()
TerminateHandler
End Sub
Private Sub Form_Unload(Cancel As Integer)
Form2Unloaded = True
End Sub
- Add a module to the project.
- Paste the following code into Module1:
Dim Form1Unloaded
Dim Form2Unloaded
Dim ExecTerminateRoutine
Sub TerminateHandler()
If Form1Unloaded And Form2Unloaded And Not (ExecTerminateRoutine) Then
ExecTerminateRoutine = True
' Execute termination code in
' the order required.
End If
End Sub
Sub Main()
Form1Unloaded = True
Form2Unloaded = True
ExecTerminateRoutine = False
Form1.Show
End Sub
- In the Project Properties dialog box, change the Startup object to Sub Main.
- Press the F5 key to run the program.
- Click on Form1 and notice that Form2 is displayed.
- Close Form2.
- Close Form1.
- After closing Form1, the TerminateHandler routine is called and the code within the If statement is executed once.
Modification Type: | Major | Last Reviewed: | 10/20/2003 |
---|
Keywords: | kbprb kbToolkit KB250477 |
---|
|