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

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.

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