|
|
Title | Perform a sequence of timed actions without multi-threading |
Description | This example shows how to perform a sequence of timed actions without multi-threading in Visual Basic 6. |
Keywords | action, timed, timer, event |
Categories | Software Engineering |
|
|
The variable m_NextEventNumber tracks the ID of the next action to execute.
When you click the Go button, the program sets m_NextEventNumber = 0 to start with the first action and then invokes the tmrNextEvent_Timer event handler.
The tmrNextEvent_Timer event handler uses a Select Case statement to see which action it should perform next. It displays the elapsed time since it performed teh last action, performs the new action, sets the Timer control's Interval property to the desired pause after this action, and then records the current time.
After the Select Case loop, the program increments the action ID number m_NextEventNumber.
|
|
Private m_NextEventNumber As Integer
' Start the event sequance.
Private Sub cmdGo_Click()
cmdGo.Enabled = False
Screen.MousePointer = vbHourglass
' Start with event 0.
m_NextEventNumber = 0
tmrNextEvent_Timer
End Sub
' Each time this fires, perform an action,
' set the next wait time,
' and increment m_NextEventNumber.
Private Sub tmrNextEvent_Timer()
Static last_time As Single
Select Case m_NextEventNumber
Case 0 ' Action 0 and wait 0.1 second.
txtResults.Text = "Start" & vbCrLf
tmrNextEvent.Interval = 100
tmrNextEvent.Enabled = True
last_time = Timer
Case 1 ' Action 1 and wait 0.5 second.
txtResults.Text = txtResults.Text & "Action 1: " & _
"100, " & _
Format$(CInt(1000 * (Timer - last_time)), _
"@@@@") & vbCrLf
tmrNextEvent.Interval = 500
last_time = Timer
Case 2 ' Action 2 and wait 2 seconds.
txtResults.Text = txtResults.Text & "Action 2: " & _
"500, " & _
Format$(CInt(1000 * (Timer - last_time)), _
"@@@@") & vbCrLf
tmrNextEvent.Interval = 2000
last_time = Timer
Case 3 ' Action 3 and wait 1 second.
txtResults.Text = txtResults.Text & "Action 3: " & _
"2000, " & _
Format$(CInt(1000 * (Timer - last_time)), _
"@@@@") & vbCrLf
tmrNextEvent.Interval = 1000
last_time = Timer
Case 4 ' Action 4 and wait 1.5 seconds.
txtResults.Text = txtResults.Text & "Action 4: " & _
"1000, " & _
Format$(CInt(1000 * (Timer - last_time)), _
"@@@@") & vbCrLf
tmrNextEvent.Interval = 1500
last_time = Timer
Case 5 ' Action 5 and wait 0.5 second.
txtResults.Text = txtResults.Text & "Action 5: " & _
"1500, " & _
Format$(CInt(1000 * (Timer - last_time)), _
"@@@@") & vbCrLf
tmrNextEvent.Interval = 500
last_time = Timer
Case 6 ' Action 6 and stop.
txtResults.Text = txtResults.Text & "Action 6: " & _
"500, " & _
Format$(CInt(1000 * (Timer - last_time)), _
"@@@@") & vbCrLf
txtResults.Text = txtResults.Text & "Done"
tmrNextEvent.Enabled = False
' Re-enable the button.
cmdGo.Enabled = True
Screen.MousePointer = vbDefault
End Select
m_NextEventNumber = m_NextEventNumber + 1
End Sub
|
|
|
|
|
|