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

MtCoffee.Vbp modMt.Bas Option Explicit

' This module demonstrates that Visual Basic creates a separate instance of global data for each thread. Thus, the glngGlobalData variable will have a separate value for each thread Visual Basic starts.

' This fact is used to keep a count of Coffee objects on each thread: In the Coffee object's Initialize event, it adds one to glngGlobalData; in its Terminate event, it subtracts one.

' Any Coffee object can then find out how many Coffee objects are on its thread by testing glngGlobalData. Clients can also find out by calling the NumberOnThread method, which returns glngGlobalData.

' When MTCoffee is run in the development environment, where there's only a single thread, NumberOnThread is the total number of Coffee objects. When MTCoffee is compiled with Thread Per Object selected, the count will be one (1) for each Coffee object, unless you call GetCoffeeOnSameThread to create a second Coffee on the thread.

' When MTCoffee is compiled with thread pooling, NumberOnThread will be greater than one on some thread whenever the number of active Coffee objects is greater than the number of threads in the pool.'

' This subject is covered in "Scalability and Multithreading," in Books Online.

Public glngGlobalData As Long

Coffee.Bas

Option Explicit

Private Declare Function timeGetTime Lib "winmm.Dll" () As Long

' The Coffee object represents a different style of asynchronous notifications from those performed by the CoffeeMonitors. Instead of periodic notifications Coffee provides progress reports on a long task, and a completion event.'

' The mechanism used for these notifications is to raise events. You could also use call-back methods, and in fact there are advantages to doing so. Call-backs would allow a component to deal intelligently with errors in the client, whereas events don't return client errors. This is discussed in "When to Use Events or Call-Backs for Notifications," in Books Online.

' Number of iterations to perform in the dummy task.

Private mlngIterations As Long

' XTimer is used to kick the long task off asynchronously.

Private WithEvents mwXTimer As XTimer

Event Progress(ByVal PercentDone As Single, ByRef Cancel As Boolean)

Event Complete(ByVal Canceled As Boolean)

' ThreadID returns the system thread ID of the thread the object was created on.

Public Property Get ThreadID() As Long

ThreadID = App.ThreadID

End Property

' NumberOnThread returns the number of Coffee objects running on this thread. This is just the value of the global data variable glngGlobalData, which Coffee objects increment in their Initialize events and decrement in their Terminate events.

' If MTCoffee was compiled with Thread Per Object, the only way for multiple objects to share a thread (and the instance of global data associated with it) is if another Coffee has been created on this thread by calling GetCoffeeOnSameThread.'

' If MTCoffee was compiled with a Thread Pool and the count of active objects exceeded the number of threads in the pool, then Coffee objects will be sharing threads.'

Public Property Get NumberOnThread() As Long

NumberOnThread = glngGlobalData

End Property

' StartLongTask sets things up for the long dummy task. The task is actually started by a code-only XTimer that StartLongTask sets running.

'

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