- •Лекция 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()
Private Sub mwCoffee_Progress(ByVal PercentDone As Single, Cancel As Boolean)
frmThread.lstResults.AddItem ThreadID _
& " (" & Size & ") " _
& Format$(PercentDone * 100, "#0.0") & "%", 0
' As each CoffeeTracker notices the global Cancel flag, it turns off the long task it's been watching.
' The Coffee object will then raise a Complete event (see above).
If frmThread.CancelAll Then Cancel = True
End Sub
NotifyMe.cls
Option Explicit
' NotifyMe implements ICoffeeNotify so that its call-back method (CoffeeReady) can be called early-bound.
Implements ICoffeeNotify
' Storage for implementation of NotifyID.
Private mlngNotifyID As Long
' ICoffeeNotify.CoffeeReady is called by the CoffeeMonitor2 object in Coffee2. This implementation
' adds the time of the call to a list box, and deletes the oldest entry if there are more than ten entries.
Private Sub ICoffeeNotify_CoffeeReady()
With Form1.lstCallBacks
.AddItem Format$(Now, "ddd hh:mm:ss"), 0
If .ListCount > 10 Then .RemoveItem 10
End With
End Sub
' ICoffeeNotify.NotifyID property holds the notification object's key in CoffeeMonitor2's array
' of client objects. Client implementation simply stores and returns the value.
Private Property Let ICoffeeNotify_NotifyID(ByVal RHS As Long)
mlngNotifyID = RHS
End Property
Private Property Get ICoffeeNotify_NotifyID() As Long
ICoffeeNotify_NotifyID = mlngNotifyID
End Property
Form1.frm
Option Explicit
'Module-level storage for the shared CoffeeMonitor object. The variable is declared WithEvents so that the CoffeeMonitor's events can be handled.
Private WithEvents mwcmnEvents As CoffeeMonitor
'Connector object used to obtain a reference to the shared CoffeeMonitor.
Private mcctEvents As Connector
'Module-level storage for a reference to the CoffeeMonitor2 object used in the Call-Back Method demo.
Private mcm2CallBacks As CoffeeMonitor2
'Storage for a reference to the call-back object.
Private mNotifyMe As New NotifyMe
'Command button used to turn event reception on and off.
Private Sub cmdEvents_Click()
Static blnInUse As Boolean
If blnInUse Then
'Setting a WithEvents variable to Nothing disconnects the object from its event procedures, so that no more events are received.
Set mwcmnEvents = Nothing
'Dispose of the Connector object.
Set mcctEvents = Nothing
cmdEvents.Caption = "Start Receiving CoffeeReady Events"
Else
Set mcctEvents = New Connector
'The Connector object supplies a reference to the shared CoffeeMonitor. When the reference is place in the WithEvents
'variable, the object is connected to its event procedure, so that CoffeeWatch can begin receiving events.
Set mwcmnEvents = mcctEvents.CoffeeMonitor
cmdEvents.Caption = "STOP Receiving CoffeeReady Events"
End If
blnInUse = True Xor blnInUse
End Sub
' Command button used to turn call-backs on and off.
Private Sub cmdCallBacks_Click()
Static blnInUse As Boolean
Dim ct2 As New Connector2
If blnInUse Then
' Tell CoffeeMonitor2 that call-backs are no longer desired.
Call mcm2CallBacks.CeaseCallBacks(mNotifyMe)
' Release the shared copy of CoffeeMonitor2.
Set mcm2CallBacks = Nothing
cmdCallBacks.Caption = "Start Receiving Call-Backs"
Else
' Obtain a Connector2 object, and use it to get a reference to the shared copy of CoffeeMonitor2.
Set ct2 = New Connector2
Set mcm2CallBacks = ct2.CoffeeMonitor2
'
' Tell CoffeeMonitor2 to begin making calls to the NotifyMe object (the object is implicitly created here, because the variable is declared As New).
Call mcm2CallBacks.TellMeReady(mNotifyMe)
cmdCallBacks.Caption = "STOP Receiving Call-Backs"
End If
blnInUse = True Xor blnInUse
End Sub