- •Лекция 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()
Public Property Get CoffeeMonitor() As CoffeeMonitor
' If the shared CoffeeMonitor object hasn't been created, create it and store a global reference to it.
If gCoffeeMonitor Is Nothing Then
Set gCoffeeMonitor = New CoffeeMonitor
End If
Set CoffeeMonitor = gCoffeeMonitor
End Property
CoffeeMonitor.Cls Option Explicit
' > For an overview of this sample application, search online Help for Coffee. AboutCof.Txt, in the Related Documents folder of CoffWat2.vbp, also contains information about the sample. CoffeeMonitor class monitors an imaginary serial interface to a high-tech coffee pot, using a call-back timer (XTimer object) to determine how often to check the coffee status. When the coffee's ready, the CoffeeMonitor object raises an event to notify clients. (Since the high-tech coffee pot has not yet been invented, this sample application just raises the CoffeeReady event every ten seconds.)
' (See also the CoffeeMonitor2 class, which demonstrates notification by a call-back method instead of an event.)'
' Note that the CoffeeMonitor class's Instancing property is set to PublicNotCreatable. This means that clients cannot create CoffeeMonitors; they can only get a reference to the shared CoffeeMonitor by creating a Connector object and accessing its CoffeeMonitor property.
' By using a code-only timer, this version of CoffeeMonitor fixes the bug described in the topic "Using the Shared CoffeeMonitor," in "Creating an ActiveX Exe Component," in Books Online, whereby multiple CoffeeMonitor objects could sometimes be created.'
' That bug was caused by the 'use count' code the Connector object used to release the global reference to the shared CoffeeMonitor. The use count was required because the step-by-step procedures in Books Online used a hidden form to hold a Timer control; hidden forms keep components from unloading (as described in "Starting and Ending a Component" in Books Online), which keeps global memory from being freed, which keeps gCoffeeMonitor from being set to Nothing.
' Unlike the hidden form with its Timer control, the XTimer object won't keep the Coffee2 component from unloading when the last client releases its last reference. (Note, however, that the global variable gCoffeeMonitor will keep the CoffeeMonitor object alive until all objects provided by Coffee2 are released by their respective clients.)
' =======================================================
' WARNING! Code-only timers are inherently dangerous in the Visual Basic development environment, becaue the system blindly calls back into your code until the timer is turned off with an API call. It's safer to use Timer controls during most of the development process, and only switch to call-back timers at the very end.
' =======================================================
' mXTimer holds a reference to a code-only timer that tells CoffeeMonitor when to check the pot. Because the variable is declared WithEvents, the CoffeeMonitor object receives the XTimer object's Tick events (see Sub mwXTimer_Tick, below). Code for the XTimer object can be found in XTimers.vbp.