Microsoft KB Archive/250477

= PRB: Terminate Events Fire in Reverse Order in VBCE/eVB =

Article ID: 250477

Article Last Modified on 10/20/2003

-

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.</li> Press the F5 key to run the program.</li> Click on Form1 and notice that Form2 is displayed.</li> Close Form2.</li> Close Form1.</li> After closing Form1, the TerminateHandler routine is called and the code within the If statement is executed once.</li></ol>

<div class="moreinformation_section">

MORE INFORMATION
This behavior only occurs in the Windows CE Platform.

Steps to Reproduce Behavior
<ol> Start a new Windows CE Project in Visual Basic. Form1 is created by default.</li> On the Projects menu, select Components, and then select Microsoft CE File System Control.</li> Add a File control to Form1.</li>  Paste the following code into Form1: Private Sub Form_Click Form2.Show End Sub

Private Sub Form_Terminate File1.Open "terminate.txt", fsModeAppend File1.WriteFields "Form1 Terminate" File1.Close End Sub </li> Add a second form to the project.</li> Add a File control to Form2.</li>  Add the following code behind Form2. Private Sub Form_Terminate File1.Open "terminate.txt", fsModeAppend File1.WriteFields "Form2 Terminate" File1.Close End Sub </li> Press the F5 key to run the program.</li> Click on Form1 and note that Form2 is displayed.</li> Close Form2.</li> <li>Close Form1.</li> <li>Open the text file that has been created (terminate.txt) and note that the Terminate event of Form2 has been executed prior to the Terminate event of Form1.</li></ol>

Additional query words: wince vbce vbce6 eVB

Keywords: kbtoolkit kbprb KB250477

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.