- •Лекция 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()
Private Sub cmdMt_Click()
' When switching to the multithreading demo, the call-backs and events should be disabled. Ask the user if its okay
' to do that. If the answer is vbNo, go ahead and start the multithreading demo anyway.
If (Not mwcmnEvents Is Nothing) Or _
(Not mcm2CallBacks Is Nothing) Then
Select Case MsgBox("Event and Call-Back notifications should be shut down before beginning the processor-intensive threading demo. Shut them down now?", _
vbYesNoCancel, "Start Multithreading Demo")
Case vbYes
' This doesn't unload the form, it just stops the demos.
Call Form_Unload(False)
Case vbCancel
Exit Sub
End Select
End If
cmdMT.Enabled = False
frmThread.Show vbModeless
End Sub
' Shut down any running demos.
Private Sub Form_Unload(Cancel As Integer)
If Not mwcmnEvents Is Nothing Then
Call cmdEvents_Click
End If
If Not mcm2CallBacks Is Nothing Then
Call cmdCallBacks_Click
End If
End Sub
' When the CoffeeMonitor object sends a CoffeeReady event, add it to the list box. If there are more than ten items in the list box, delete the oldest.
Private Sub mwcmnEvents_CoffeeReady()
With lstEvents
.AddItem Format$(Now, "ddd hh:mm:ss"), 0
If .ListCount > 10 Then .RemoveItem 10
End With
End Sub
FrmThread.Frm
Option Explicit
' The multithreading demo:
' - Shows thread ids and number of objects on a thread (this will be more interesting if you compile MTCoffee.exe with a thread pool size of 3 or 4).
' - Compares call overhead for same-thread vs. cross-thread calls.
' - Time per iteration for serial short tasks (see Coffee object defined in MTCoffee.cls).
' - Times per iteration for a long task, and for serial short tasks run at the same time.
' - Times per iteration for a number of long tasks, with serial short tasks running at the same time.
'
' You can add your own tests to this framework.
' You may find it interesting to compare the behavior of tasks that block (such as database queries on remote computers, or large file transfers). On a computer with a single processor, such tasks will behave much better than the computation-intensive tasks used in the examples above. Threads that perform computation-intensive tasks compete with each other for the machine's single processor, and so their performance suffers as the number of active threads increases.
'
' For more information, see "Scalability and Multithreading," in "Building Code Components" in Books Online.
' These constants control the relative size of a long task and a short task. You may need to adjust these for the speed of your processor.
Const SHORTTASKSIZE = 50000
Const LONGTASKSIZE = 2000000
' Array of Coffee objects.
Private macfe(1 To 20) As Coffee
' Collection of CoffeeTracker objects.
Public CoffeeTrackers As New Collection
' Cancel flag.
Public CancelAll As Boolean
' How many short tasks to run.
Private mintHowManyShort As Integer
Private Sub cmdCancel_Click()
' When the user clicks Cancel, set a flag that CoffeeTracker can use to cancel all long tasks when they raise their next Progress event.
CancelAll = True
End Sub
' Run a few short tasks serially, to get a feel for their speed when not competing for the processor.