Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лек 013.doc
Скачиваний:
21
Добавлен:
07.02.2015
Размер:
137.73 Кб
Скачать

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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]