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

Private WithEvents mwXTimer As xTimer

' CoffeeReady is the event CoffeeMonitor raises for its clients when the coffee is done. The Public keyword is omitted from the event declaration because events are always public.

Event CoffeeReady() Private Sub Class_Initialize()

' The first thing a CoffeeMonitor object does is create the XTimer object. When this assignment is made, Visual Basic connects the XTimer's Tick event to the mwXTimer_Tick event procedure (see below).'

Set mwXTimer = New XTimer

' The timer is set to tick every ten seconds (10,000 milliseconds).

mwXTimer.Interval = 10000

mwXTimer.Enabled = True

End Sub

Private Sub Class_Terminate()

' It's important to disable the XTimer before releasing it. As described in XTimers.vbp, abandoning a running XTimer essentially leaks a system timer until XTimers.DLL finally shuts down.

mwXTimer.Enabled = False

Set mwXTimer = Nothing

'

Debug.Print "CoffeeMonitor (events) terminated at " & Now

End Sub

' mwXTimer_Tick is the event procedure CoffeeMonitor uses to receive the XTimer object's Tick events. The name of an event procedure that's associated with a WithEvents variable always has the variable name as a prefix.

'

Private Sub mwXTimer_Tick()

' (Code to test serial port omitted.)

'

' Notify the client.

RaiseEvent CoffeeReady

End Sub

Connector2.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.

' Connector2 class allows multiple clients to share a single instance of CoffeeMonitor2. The Connector2 class has its Instancing property set to MultiUse, so each client can create its own Connector2. All the Connector objects return a reference to the single shared CoffeeMonitor2 object, so all the clients share the same CoffeeMonitor2. (See the CoffeeMonitor2 property, below.)

' CoffeeMonitor2 property always returns the single global reference to the shared instance of CoffeeMonitor2.

Public Property Get CoffeeMonitor2() As CoffeeMonitor2

' If the shared CoffeeMonitor object hasn't been created, create it and store a global reference to it.

If gCoffeeMonitor2 Is Nothing Then

Set gCoffeeMonitor2 = New CoffeeMonitor2

End If

Set CoffeeMonitor2 = gCoffeeMonitor2

End Property

CoffeeMonitor2 Option Explicit

' > Для обзора этого примера смотри online Help for Coffee. AboutCof.Txt, в Related Documents директории of CoffWat2.vbp, также содержит информацию о примере.

' CoffeeMonitor2 class--------

' Подобно объекту CoffeeMonitor , CoffeeMonitor2 наставляет воображаемый параллельный интерфейс к высокотехничной чашке кофе, используя таймер для определения как часто проверять статус кофе.

' Вместо реализации зажигания события когда готов кофе, CoffeeMonitor2 вызавает a call-back метод который должен бать реализован в одном из классов клиента. Call-back method объявлен в ICallBack классе.

' (Поскольку высокотехничнфя чашке кофе пока еще не придумана, то этот пример просто вызывает call-back метод каждые десять секунд.)

' IMPORTANT: To simplify a rather complex example, the ICallBack class has been included in this project. This will NOT work in real-life systems, which usually go through many versions. If a second version of Coffee2 is made, the interface ID version number will be incremented, and earlier clients will NOT work with the new version of Coffee2. Standard interfaces like ICallBack should be created by themselves in small DLLs which can be referenced by both client and component. Once an interface is in use by finished applications, it must never be changed. For more information, search for "polymorphism" in Books Online.

' Note that the CoffeeMonitor2 class's Instancing property is set to PublicNotCreatable. This means that clients cannot create a CoffeeMonitor2; they can only get a reference to the shared CoffeeMonitor2 by creating a Connector2 object and accessing its CoffeeMonitor2 property.

'' Like the CoffeeMonitor class, the CoffeeMonitor2 class 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.

' =======================================================

' 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.

' =======================================================

Const ICN_ARRAYINCREMENT = 10

' maicnClients stores references to all the clients that have requested call-backs. (Note that this is different from the use of events in CoffeeMonitor; one event can be received by any number of clients, while call-backs must be made one by one.) An array is used, rather than a Collection, because Collection objects keep objects in Variants, resulting in late binding.

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