- •Лекция 13
- •XTimers.Vbp 5
- •Обзор задачи Coffee Sample Application
- •XTimers.Vbp
- •XTimerSupport.Bas
- •Private Type xtimerinfo ' Hungarian xti
- •Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal lngSysTime As Long)
- •If maxti(intCt).BlnReentered Then Exit Sub
- •Public Sub EndTimer(ByVal xt As xTimer)
- •Public Sub Scrub()
- •Public Property Get Interval() As Long
- •Public Property Let Interval(ByVal NewInterval As Long)
- •Public Sub RaiseTick()
- •Private Sub Class_Terminate()
- •MtCoffee.Vbp modMt.Bas Option Explicit
- •Public glngGlobalData As Long
- •Coffee.Bas
- •Option Explicit
- •Private Declare Function timeGetTime Lib "winmm.Dll" () As Long
- •Private mlngIterations As Long
- •Public Property Get NumberOnThread() As Long
- •Public Sub StartLongTask(ByVal Iterations As Long)
- •Public Function GetCoffeeOnSameThread() As Coffee
- •Public Function GetCoffeeOnNewThread() As Coffee
- •If blnCancel Then
- •Coffee.Cls Option Explicit Private Declare Function timeGetTime Lib "winmm.Dll" () As Long
- •Private mlngIterations As Long
- •Public Property Get NumberOnThread() As Long
- •Public Sub StartLongTask(ByVal Iterations As Long)
- •Public Function GetCoffeeOnSameThread() As Coffee
- •Public Function GetCoffeeOnNewThread() As Coffee
- •If blnCancel Then
- •Public Property Get CoffeeMonitor() As CoffeeMonitor
- •If gCoffeeMonitor Is Nothing Then
- •CoffeeMonitor.Cls Option Explicit
- •Private WithEvents mwXTimer As xTimer
- •Event CoffeeReady() Private Sub Class_Initialize()
- •Private Sub Class_Terminate()
- •Private Sub mwXTimer_Tick()
- •Connector2.Cls Option Explicit
- •Public Property Get CoffeeMonitor2() As CoffeeMonitor2
- •CoffeeMonitor2 Option Explicit
- •Private maicnClients() As iCoffeeNotify
- •Public Sub TellMeReady(ByVal icn As iCoffeeNotify)
- •Public Sub CeaseCallBacks(ByVal icn As iCoffeeNotify)
- •Private Sub mwXTimer_Tick()
- •ICoffeeNotify.Cls
- •Public ThreadId As Long
- •Private Sub mwCoffee_Progress(ByVal PercentDone As Single, Cancel As Boolean)
- •Private Sub cmdCallBacks_Click()
- •Private Sub cmdMt_Click()
- •Private Sub Form_Unload(Cancel As Integer)
- •Private Sub mwcmnEvents_CoffeeReady()
- •FrmThread.Frm
- •Private Sub cmdCancel_Click()
- •Private Sub cmdShortOnly_Click()
- •Private Sub cmdShortLong_Click()
- •Private Sub cmdNLong_Click()
- •Private Sub cmdXThread_Click()
- •Private Sub cmdIDs_Click()
- •Private Sub tmrShort_Timer()
ICoffeeNotify.Cls
Option Explicit
' The ICoffeeNotify module defines an interface containing the call-back method, CoffeeReady, and a property used to store the client's ID (assigned by CoffeeMonitor2). CoffeeReady implemention contains all the code the client wants to execute when a call-back occurs.'
Public Sub CoffeeReady()
End Sub
' NotifyID implementation must provide a Long to store the ID number. CoffeeMonitor2 uses this to look up the call-back object when a request to disconnect is made.
Public Property Get NotifyID() As Long
End Property
Public Property Let NotifyID(ByVal NewValue As Long)
End Property
CoffeeWatch2.vbp.
Module1.bas
Option Explicit
Declare Function timeGetTime Lib "winmm.dll" () As Long
' CoffeeWatch must be an ActiveX Exe, because it exposes the public NotifyMe object used for the call-back method demo. It will never be started as a server, only as a standalone application.
Sub Main()
If App.StartMode = vbSModeStandalone Then
Form1.Show
End If
End Sub
CoffeeTracker.cls
Option Explicit
' CoffeeTracker is a small private object for keeping track of a task a Coffee object is executing. The reason for using an object is that WithEvents variables cannot be in arrays. In order to have a varying number of them, you have to have a class of objects to keep the WithEvents variables.
' Set the ThreadID and Size before you start the long task; set the Coffee property JUST before you call StartLongTask. ID is assigned by the NewTracker procedure in frmThread; it's the object's index in the CoffeeTrackers Collection object.
Public ThreadId As Long
Public Size As Long
Public ID As String
' Storage for the Coffee object being tracked.
Private WithEvents mwCoffee As Coffee
' Start time (from timeGetTimer API).
Private mlngStart As Long
Public Property Get Coffee() As Coffee
Set Coffee = mwCoffee
End Property
Public Property Set Coffee(ByVal NewValue As Coffee)
' Save the start time.
mlngStart = timeGetTime
Set mwCoffee = NewValue
End Property
' The Coffee object raises a Complete event when the task being tracked is complete. CoffeeTracker puts information about the task (thread ID, size, and seconds per iteration) into a list box on frmThread.
Private Sub mwCoffee_Complete(ByVal Canceled As Boolean)
Dim lngEnd As Long
Dim dblElapsed As Double
lngEnd = timeGetTime
'
' Free the Coffee object.
Set mwCoffee = Nothing
'
' Add a report line to the list box.
If Canceled Then
frmThread.lstResults.AddItem ThreadID _
& " (" & Size & ") canceled", 0
Else
frmThread.lstResults.AddItem ThreadID _
& " (" & Size & ") " _
& (CDbl(lngEnd) - mlngStart) / Size / 1000# _
& " sec/iteration", 0
End If
'
' CoffeeTracker removes its reference from the collection, leaving itself without references -- so that it can terminate.
frmThread.CoffeeTrackers.Remove ID
End Sub
' For long tasks, Coffee events raise a Progress event for every 10% of the task it completes. CoffeeTracker adds an entry to the list box on frmThread.