如小面的代码:
Private Sub Cmd_TestStart_Click() Do While (isStoped = False)
DoEvents
'read STARTEST Status
If ReadData(CurrentPID, STARTEST, RData, 1) Then
If RData(0) = &H80& Then
isStoped = True
Call RestoreGUIState
Form_Main.StatusBar1.Panels(2).text = "Test stopped. "
Exit Do
End If
Else
isStoped = True
Call RestoreGUIState
Form_Main.StatusBar1.Panels(2).text = "Test Error, stopped. "
Exit Sub
End If
'read error result
If Not ReadData(CurrentPID, ERRRESUT, RData, 15) Then
isStoped = True
Call RestoreGUIState
Form_Main.StatusBar1.Panels(2).text = "Test Error, stopped. "
Exit Sub
End If
'Calc and show to Form
Call GetEquipment(RData)
Call GetStatu(RData(14))
Call Delay_Ms(1000)
Loop
End SubPrivate Sub Cmd_TestStop_Click()
isStoped = True 'Set stop flag
End Sub
Private Sub Cmd_TestStart_Click() Do While (isStoped = False)
DoEvents
'read STARTEST Status
If ReadData(CurrentPID, STARTEST, RData, 1) Then
If RData(0) = &H80& Then
isStoped = True
Call RestoreGUIState
Form_Main.StatusBar1.Panels(2).text = "Test stopped. "
Exit Do
End If
Else
isStoped = True
Call RestoreGUIState
Form_Main.StatusBar1.Panels(2).text = "Test Error, stopped. "
Exit Sub
End If
'read error result
If Not ReadData(CurrentPID, ERRRESUT, RData, 15) Then
isStoped = True
Call RestoreGUIState
Form_Main.StatusBar1.Panels(2).text = "Test Error, stopped. "
Exit Sub
End If
'Calc and show to Form
Call GetEquipment(RData)
Call GetStatu(RData(14))
Call Delay_Ms(1000)
Loop
End SubPrivate Sub Cmd_TestStop_Click()
isStoped = True 'Set stop flag
End Sub
DoEvents相当于在当前过程内部调用了事件处理函数(如果有新的事件产生)。因此和递归调用很类似,要避免两点,一个是全局变量的冲突,一个是无限递归。要避免前者,一个是少用或者不用全局变量,尤其是控件、窗体。一个是如果非要使用,应该用一个变量将全局变量当前值保存起来,在事件处理完毕后恢复,以便外层函数的正确调用。要避免无限递归,就是要避免在DoEvents中直接触发新的相同的事件。