home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / articles / vbpj / source / eventcod.frm (.txt) next >
Encoding:
Visual Basic Form  |  1996-01-29  |  9.8 KB  |  302 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H80000005&
  5.    Caption         =   "Event Driven Programming Example"
  6.    ClientHeight    =   3705
  7.    ClientLeft      =   1365
  8.    ClientTop       =   1890
  9.    ClientWidth     =   4515
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   4110
  21.    Left            =   1305
  22.    LinkMode        =   1  'Source
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   3705
  25.    ScaleWidth      =   4515
  26.    Top             =   1545
  27.    Width           =   4635
  28.    Begin VB.CommandButton CmdVerify 
  29.       Appearance      =   0  'Flat
  30.       BackColor       =   &H80000005&
  31.       Caption         =   "Verify Sort"
  32.       Height          =   495
  33.       Left            =   2160
  34.       TabIndex        =   7
  35.       Top             =   3120
  36.       Width           =   2175
  37.    End
  38.    Begin VB.CommandButton CmdQsortBackground 
  39.       Appearance      =   0  'Flat
  40.       BackColor       =   &H80000005&
  41.       Caption         =   "QuickSort - Background"
  42.       Height          =   495
  43.       Left            =   2160
  44.       TabIndex        =   9
  45.       Top             =   2520
  46.       Width           =   2175
  47.    End
  48.    Begin VB.CommandButton CmdCancelSort 
  49.       Appearance      =   0  'Flat
  50.       BackColor       =   &H80000005&
  51.       Caption         =   "Cancel Sort"
  52.       Height          =   495
  53.       Left            =   840
  54.       TabIndex        =   10
  55.       Top             =   2520
  56.       Width           =   1215
  57.    End
  58.    Begin VB.Timer Timer1 
  59.       Enabled         =   0   'False
  60.       Interval        =   1
  61.       Left            =   240
  62.       Top             =   2400
  63.    End
  64.    Begin VB.CommandButton CmdQsortNoEvents 
  65.       Appearance      =   0  'Flat
  66.       BackColor       =   &H80000005&
  67.       Caption         =   "QuickSort - No Events"
  68.       Height          =   495
  69.       Left            =   2160
  70.       TabIndex        =   8
  71.       Top             =   1920
  72.       Width           =   2175
  73.    End
  74.    Begin VB.CommandButton CmdSrchBackground 
  75.       Appearance      =   0  'Flat
  76.       BackColor       =   &H80000005&
  77.       Caption         =   "SearchEventfully"
  78.       Height          =   495
  79.       Left            =   2160
  80.       TabIndex        =   6
  81.       Top             =   1320
  82.       Width           =   2175
  83.    End
  84.    Begin VB.CommandButton CmdSrchDoEvents 
  85.       Appearance      =   0  'Flat
  86.       BackColor       =   &H80000005&
  87.       Caption         =   "SearchWithDoEvents"
  88.       Height          =   495
  89.       Left            =   2160
  90.       TabIndex        =   3
  91.       Top             =   720
  92.       Width           =   2175
  93.    End
  94.    Begin VB.CommandButton CmdSrchNoEvents 
  95.       Appearance      =   0  'Flat
  96.       BackColor       =   &H80000005&
  97.       Caption         =   "SearchWithoutEvents"
  98.       Height          =   495
  99.       Left            =   2160
  100.       TabIndex        =   2
  101.       Top             =   120
  102.       Width           =   2175
  103.    End
  104.    Begin VB.Label backlabel 
  105.       Alignment       =   2  'Center
  106.       Appearance      =   0  'Flat
  107.       BackColor       =   &H80000005&
  108.       ForeColor       =   &H80000008&
  109.       Height          =   255
  110.       Left            =   120
  111.       TabIndex        =   5
  112.       Top             =   1680
  113.       Width           =   1695
  114.    End
  115.    Begin VB.Label Label2 
  116.       Appearance      =   0  'Flat
  117.       BackColor       =   &H80000005&
  118.       Caption         =   "Background Status"
  119.       ForeColor       =   &H80000008&
  120.       Height          =   255
  121.       Left            =   120
  122.       TabIndex        =   4
  123.       Top             =   1320
  124.       Width           =   1695
  125.    End
  126.    Begin VB.Label ResultLabel 
  127.       Alignment       =   2  'Center
  128.       Appearance      =   0  'Flat
  129.       BackColor       =   &H80000005&
  130.       ForeColor       =   &H80000008&
  131.       Height          =   255
  132.       Left            =   120
  133.       TabIndex        =   0
  134.       Top             =   840
  135.       Width           =   1815
  136.    End
  137.    Begin VB.Label Label1 
  138.       Appearance      =   0  'Flat
  139.       BackColor       =   &H80000005&
  140.       Caption         =   "Search Result"
  141.       ForeColor       =   &H80000008&
  142.       Height          =   255
  143.       Left            =   120
  144.       TabIndex        =   1
  145.       Top             =   480
  146.       Width           =   1335
  147.    End
  148. Attribute VB_Name = "Form1"
  149. Attribute VB_Creatable = False
  150. Attribute VB_Exposed = False
  151. ' EventCod test program.
  152. ' Copyright (c) 1992, by Desaware
  153. ' Indicates the operation of timer 1 on this form.
  154. ' 0 indicates no operation
  155. ' 1 indicates initializing is in progress
  156. ' 2 indicates SearchEventfully is in progress
  157. ' 3 indicates Background sorting is in progress
  158. Dim TimerMode%
  159. Private Sub CmdCancelSort_Click()
  160.     t% = QSortBackground%(-1, 0)
  161. End Sub
  162. Private Sub CmdQsortBackground_Click()
  163.     Dim t%
  164.     ' The following line will prevent reentry - Refer
  165.     ' to the article for alternate approaches such
  166.     ' as starting a new sort.
  167.     If TimerMode% Then
  168.         MsgBox "Background operation already in progress"
  169.         Exit Sub
  170.         End If
  171.     t% = QSortBackground%(1, ArraySize%)
  172.     If t% < 0 Then ' Start event driven sorting
  173.         backlabel.Caption = "Sorting"
  174.         CmdCancelSort.Enabled = -1
  175.         TimerMode% = 3
  176.         Timer1.Enabled = -1
  177.     Else
  178.         If t% = 0 Then
  179.             ResultLabel.Caption = "Sorting complete"
  180.         Else
  181.             ResultLabel.Caption = "Sorting cancelled"
  182.             End If
  183.         End If
  184. End Sub
  185. '   Perform a QuickSort without events
  186. Private Sub CmdQsortNoEvents_Click()
  187. Dim oldmouseptr%
  188.     oldmouseptr% = Screen.MousePointer
  189.     Screen.MousePointer = 11
  190.     QSortNoEvents 1, ArraySize
  191.     Screen.MousePointer = oldmouseptr%
  192. End Sub
  193. ' Search "Eventfully" - i.e. background processing
  194. Private Sub CmdSrchBackground_Click()
  195.     Dim r, t%
  196.     ' The following line will prevent reentry - Refer
  197.     ' to the article for alternate approaches such
  198.     ' as starting a new search.
  199.     If TimerMode% = 3 Then
  200.         MsgBox "Background sort in progress"
  201.         Exit Sub
  202.         End If
  203.     ' Ignore command if any other background operation is
  204.     ' in progress
  205.     If TimerMode% Then Exit Sub
  206.     r = Int(Rnd * ArraySize + 1)
  207.     t% = SearchEventfully%(r, -1)
  208.     If t% < 0 Then ' Start event driven initialization
  209.         backlabel.Caption = "Searching"
  210.         TimerMode% = 2
  211.         Timer1.Enabled = -1
  212.     Else: ResultLabel.Caption = Str$(t%)
  213.     End If
  214. End Sub
  215. '   Search with DoEvents command
  216. Private Sub CmdSrchDoEvents_Click()
  217.     Dim r
  218.     r = Int(Rnd * ArraySize + 1)
  219.     ResultLabel.Caption = Str$(SearchWithDoEvents%(r))
  220. End Sub
  221. ' Search without DoEvents command
  222. Private Sub CmdSrchNoEvents_Click()
  223.     Dim r
  224.     r = Int(Rnd * ArraySize + 1)
  225.     ResultLabel.Caption = Str$(SearchWithoutEvents%(r))
  226. End Sub
  227. Private Sub CmdVerify_Click()
  228. Dim x%
  229. Dim oldmouseptr%
  230.     oldmouseptr% = Screen.MousePointer
  231.     Screen.MousePointer = 11
  232.     For x% = 1 To ArraySize% - 1
  233.         If SampleArray(x%) > SampleArray(x% + 1) Then
  234.             ResultLabel.Caption = "Not Sorted @" + Str$(x%)
  235.             Screen.MousePointer = oldmouseptr%
  236.             Exit Sub
  237.             End If
  238.         Next x%
  239.     ResultLabel.Caption = "Sorted"
  240.     Screen.MousePointer = oldmouseptr%
  241. End Sub
  242. Private Sub Form_Load()
  243.     Dim t%
  244.     Randomize
  245.     t% = LoadSampleArray()
  246.     If t% Then ' Start event driven initialization
  247.         ' Don't allow sorting during initialization!
  248.         CmdQsortNoEvents.Enabled = 0
  249.         CmdQsortBackground.Enabled = 0
  250.         ' The cancel sort command is disabled to start
  251.         CmdCancelSort.Enabled = 0
  252.         backlabel.Caption = "Initializing"
  253.         TimerMode% = 1
  254.         Timer1.Enabled = -1
  255.     End If
  256. End Sub
  257. '   We use the timer to keep background operations
  258. '   operating properly.
  259. '   This algorithm allows only one background operation
  260. '   at a time.  See article text for ideas how maintaining
  261. '   multiple concurrent operations.
  262. Private Sub Timer1_Timer()
  263.     Dim t%, u%
  264.     Select Case TimerMode%
  265.         Case 1  ' Background initialization
  266.                 t% = LoadSampleArray%()
  267.                 If Not t% Then  ' We're done!
  268.                     TimerMode% = 0
  269.                     Timer1.Enabled = 0
  270.                     backlabel.Caption = "Idle"
  271.                     CmdQsortNoEvents.Enabled = -1
  272.                     CmdQsortBackground.Enabled = -1
  273.                 End If
  274.         Case 2  ' Background searching
  275.                 t% = SearchEventfully%(0, 0)
  276.                 If t% >= 0 Then ' We're done!
  277.                     TimerMode% = 0
  278.                     Timer1.Enabled = 0
  279.                     ResultLabel.Caption = Str$(t%)
  280.                     backlabel.Caption = "Idle"
  281.                 End If
  282.         Case 3  ' Background sorting
  283.                 ' The sort granularity is set here
  284.                 For u% = 1 To 20
  285.                     t% = QSortBackground%(0, 0)
  286.                     If t% >= 0 Then Exit For
  287.                     Next u%
  288.                 If t% >= 0 Then ' We're done!
  289.                     CmdCancelSort.Enabled = 0
  290.                     TimerMode% = 0
  291.                     Timer1.Enabled = 0
  292.                     backlabel.Caption = "Idle"
  293.                     If t% = 0 Then
  294.                         ResultLabel.Caption = "Sorting complete"
  295.                     Else
  296.                         ResultLabel.Caption = "Sorting cancelled"
  297.                         End If
  298.                     End If
  299.     End Select
  300.         
  301. End Sub
  302.