home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form Form1
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "Architecting Event Driven Code"
- ClientHeight = 3900
- ClientLeft = 1095
- ClientTop = 1485
- ClientWidth = 5580
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 1
- weight = 700
- size = 8.25
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- ForeColor = &H80000008&
- Height = 4305
- Left = 1035
- LinkTopic = "Form1"
- ScaleHeight = 3900
- ScaleWidth = 5580
- Top = 1140
- Width = 5700
- Begin VB.CommandButton Command7
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "Cancel Timer"
- Height = 435
- Left = 3060
- TabIndex = 7
- Top = 3360
- Width = 1755
- End
- Begin VB.Timer Timer1
- Enabled = 0 'False
- Interval = 1
- Left = 180
- Top = 2940
- End
- Begin VB.CommandButton Command6
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "With Timer"
- Height = 435
- Left = 3060
- TabIndex = 6
- Top = 2880
- Width = 1755
- End
- Begin VB.CommandButton Command5
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "With DoEvents III"
- Height = 495
- Left = 3060
- TabIndex = 5
- Top = 2340
- Width = 1755
- End
- Begin VB.CommandButton Command4
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "With DoEvents II"
- Height = 495
- Left = 3060
- TabIndex = 4
- Top = 1800
- Width = 1755
- End
- Begin VB.CommandButton Command3
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "With DoEvents"
- Height = 495
- Left = 3060
- TabIndex = 3
- Top = 1260
- Width = 1755
- End
- Begin VB.CommandButton Command2
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "Escape Check"
- Height = 495
- Left = 3060
- TabIndex = 2
- Top = 720
- Width = 1755
- End
- Begin VB.CommandButton Command1
- Appearance = 0 'Flat
- BackColor = &H80000005&
- Caption = "No DoEvents"
- Height = 495
- Left = 3060
- TabIndex = 1
- Top = 180
- Width = 1755
- End
- Begin VB.Label Label1
- Appearance = 0 'Flat
- BackColor = &H00FFFFFF&
- Caption = "Label1"
- BeginProperty Font
- name = "MS Sans Serif"
- charset = 1
- weight = 700
- size = 24
- underline = 0 'False
- italic = 0 'False
- strikethrough = 0 'False
- EndProperty
- ForeColor = &H80000008&
- Height = 675
- Left = 300
- TabIndex = 0
- Top = 420
- Width = 2295
- End
- Attribute VB_Name = "Form1"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- Option Explicit
- ' With no events allowed, not only are further clicks
- ' not acted upon, but they are queued up for later - leaving
- ' to results confusing to the user.
- Private Sub Command1_Click()
- Dim x&
- For x& = 1 To LOOPCOUNT
- If x& = 500 Then ToggleColor
- label1.Caption = Str$(x&)
- label1.Refresh
- Next x&
- End Sub
- ' A classic DOS approach is to check for a key such as
- ' the escape key. But this still allows queued events
- ' to pile up.
- Private Sub Command2_Click()
- Dim x&
- Dim EscapeKey%
- ' Clear the current state
- EscapeKey% = GetAsyncKeyState(VK_ESCAPE)
- For x& = 1 To LOOPCOUNT
- If x& = 500 Then ToggleColor
- label1.Caption = Str$(x&)
- label1.Refresh
- EscapeKey% = GetAsyncKeyState(VK_ESCAPE)
- If EscapeKey% And 1 Then Exit Sub
- Next x&
- End Sub
- ' This time we place a DoEvents to allow events to be
- ' processed - but note the reentrancy problem!
- Private Sub Command3_Click()
- Dim x&
- For x& = 1 To LOOPCOUNT
- label1.Caption = Str$(x&)
- If x& = 500 Then ToggleColor
- ' Note - we don't need the refresh any more
- DoEvents
- Next x&
- End Sub
- ' We can prevent reentrancy problems by disabling the form
- Private Sub Command4_Click()
- Dim x&
- ' The easy way is to disable the entire form
- Form1.Enabled = False
- ' Alternatively, you can disable each control
- ' individually (it would look better)
- For x& = 1 To LOOPCOUNT
- label1.Caption = Str$(x&)
- If x& = LOOPCOUNT Then ToggleColor
- ' Note - we don't need the refresh any more
- DoEvents
- Next x&
- ' And be sure to reenable the form when done
- Form1.Enabled = True
- End Sub
- ' The disabling might look better if we do it one control
- ' at a time
- Private Sub Command5_Click()
- Dim x&
- Dim ctlnum%
- ' Alternatively, you can disable each control
- ' individually (it would look better)
- For ctlnum% = 0 To Controls.Count - 1
- If TypeOf Controls(ctlnum%) Is CommandButton Then
- Controls(ctlnum%).Enabled = False
- End If
- Next ctlnum%
- For x& = 1 To LOOPCOUNT
- label1.Caption = Str$(x&)
- If x& = LOOPCOUNT Then ToggleColor
- ' Note - we don't need the refresh any more
- DoEvents
- Next x&
- ' And be sure to reenable the controls when done
- For ctlnum% = 0 To Controls.Count - 1
- If TypeOf Controls(ctlnum%) Is CommandButton Then
- Controls(ctlnum%).Enabled = True
- End If
- Next ctlnum%
- Form1.Enabled = True
- End Sub
- Private Sub Command6_Click()
- Dim di%
- di% = PerformCount(0)
- timer1.Enabled = True
- End Sub
- Private Sub Command7_Click()
- timer1.Enabled = False
- End Sub
- ' This is a function designed to be reentrant without being
- ' recursive.
- ' mode is 0 to initialize the counter
- ' mode is 1 to continue counting
- ' Return value is 0 if counting is finished
- ' Return value is 1 to continue counting
- Private Function PerformCount%(mode As Integer)
- Static counter&
- Select Case mode
- Case 0
- counter& = 0
- Case 1
- counter& = counter& + 1
- End Select
- If counter& = LOOPCOUNT Then
- ToggleColor
- PerformCount% = 0
- Else
- PerformCount% = 1
- End If
- label1.Caption = Str$(counter&)
- End Function
- Private Sub Timer1_Timer()
- Dim res%
- res% = PerformCount(1)
- ' Once the termination condition is reached, shut off
- ' the timer
- If res% = 0 Then timer1.Enabled = False
- End Sub
- Private Sub ToggleColor()
- If label1.BackColor = QBColor(15) Then
- label1.BackColor = QBColor(11)
- Else
- label1.BackColor = QBColor(15)
- End If
- End Sub
-