home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / COMPTOOL / ACTVCOMP / COFFEE / CWCOFTRK.CLS < prev    next >
Encoding:
Visual Basic class definition  |  1996-12-04  |  3.0 KB  |  101 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CoffeeTracker"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11.  
  12. ' CoffeeTracker is a small private object
  13. ' =============     for keeping track of
  14. '   a task a Coffee object is executing.
  15. '   The reason for using an object is that
  16. '   WithEvents variables cannot be in
  17. '   arrays.  In order to have a varying
  18. '   number of them, you have to have a
  19. '   class of objects to keep the WithEvents
  20. '   variables.
  21. '
  22. ' Set the ThreadID and Size before you start
  23. '   the long task; set the Coffee property
  24. '   JUST before you call StartLongTask.  ID
  25. '   is assigned by the NewTracker procedure
  26. '   in frmThread; it's the object's index
  27. '   in the CoffeeTrackers Collection object.
  28.  
  29. Public ThreadID As Long
  30. Public Size As Long
  31. Public ID As String
  32.  
  33. ' Storage for the Coffee object being tracked.
  34. Private WithEvents mwCoffee As Coffee
  35. Attribute mwCoffee.VB_VarHelpID = -1
  36. '
  37. ' Start time (from timeGetTimer API).
  38. Private mlngStart As Long
  39.  
  40. Public Property Get Coffee() As Coffee
  41.     Set Coffee = mwCoffee
  42. End Property
  43. Public Property Set Coffee(ByVal NewValue As Coffee)
  44.     ' Save the start time.
  45.     mlngStart = timeGetTime
  46.     Set mwCoffee = NewValue
  47. End Property
  48.  
  49. ' The Coffee object raises a Complete
  50. '   event when the task being tracked
  51. '   is complete.  CoffeeTracker puts
  52. '   information about the task (thread
  53. '   ID, size, and seconds per iteration)
  54. '   into a list box on frmThread.
  55. '
  56. Private Sub mwCoffee_Complete(ByVal Canceled As Boolean)
  57.     Dim lngEnd As Long
  58.     Dim dblElapsed As Double
  59.     
  60.     lngEnd = timeGetTime
  61.     '
  62.     ' Free the Coffee object.
  63.     Set mwCoffee = Nothing
  64.     '
  65.     ' Add a report line to the list box.
  66.     If Canceled Then
  67.         frmThread.lstResults.AddItem ThreadID _
  68.             & " (" & Size & ") canceled", 0
  69.     Else
  70.         frmThread.lstResults.AddItem ThreadID _
  71.             & " (" & Size & ") " _
  72.             & (CDbl(lngEnd) - mlngStart) / Size / 1000# _
  73.             & " sec/iteration", 0
  74.     End If
  75.     '
  76.     ' CoffeeTracker removes its reference
  77.     '   from the collection, leaving itself
  78.     '   without references -- so that it
  79.     '   can terminate.
  80.     frmThread.CoffeeTrackers.Remove ID
  81. End Sub
  82.  
  83. ' For long tasks, Coffee events raise
  84. '   a Progress event for every 10% of the
  85. '   task it completes.  CoffeeTracker adds
  86. '   an entry to the list box on frmThread.
  87. '
  88. Private Sub mwCoffee_Progress(ByVal PercentDone As Single, Cancel As Boolean)
  89.     frmThread.lstResults.AddItem ThreadID _
  90.         & " (" & Size & ") " _
  91.         & Format$(PercentDone * 100, "#0.0") & "%", 0
  92.     '
  93.     ' As each CoffeeTracker notices the
  94.     '   global Cancel flag, it turns off
  95.     '   the long task it's been watching.
  96.     '   The Coffee object will then raise
  97.     '   a Complete event (see above).
  98.     If frmThread.CancelAll Then Cancel = True
  99. End Sub
  100.  
  101.