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

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.

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