property CreateTaskQueueObject.
Creates a thread pool to
achieve tasks or jobs
|
nbWorkerThread
[in,defaultvalue(1)] . Number of worker threads
pVal
[out, retval] The returned value is a handle of
the thread pool
S_OK
The operation was successfull.
E_POINTER
A NULL pointer was supplied as an argument.
E_UNEXPECTED
An unexpected error occoured.
Here is an
use-case of a thread pool which implements a search for prime numbers according to Eratosthenes method:
-
See: Eratosthenes_Screen.vbs (VBScript)
Dim crible '...French touch :-)
Dim prime_number :
prime_number = 2
Dim offset_prime :
offset_prime = 1
Set ScriptEZ = WScript.CreateObject("ScriptEZ.API","ScriptEZ_")
highValue = ScriptEZ.InputComboBox(0,"Search
for prime numbers beyond a number, please enter a
number:|&Ok|&Cancel",1)
If Len(highValue)
<= 0 Then WScript.Quit
'-- Array of
integers factory
Set dico = CreateObject("Scripting.Dictionary")
For i=prime_number To highValue+1
dico.Add CStr(i), CStr(i)
Next
crible = dico.Items '-- extract the array
Set dico = Nothing
'-- Create a
thread pool of 1 thread
hThreadPool = ScriptEZ.CreateTaskQueueObject(1)
'-- Submit a
recurring task which implements the prime numbers search with Eratosthenes
method
r = ScriptEZ.AddTask(hThreadPool,UBound(crible)-1)
'-- Block and yield
the control to the thread pool
Do Until 0
WScript.Sleep 1000
Loop
'-- Callback
to process iteratively the search for prime numbers
Function ScriptEZ_OnRun(localStorage)
WScript.Sleep 50
Dim new_prime_number : new_prime_number = 0
Dim
multiple_found : multiple_found = false
'-- Browse across the sieve and delete not
prime numbers
For i=offset_prime To localStorage
If crible(i) <> prime_number And
crible(i) <> 0 And (crible(i) Mod prime_number) = 0 Then
ScriptEZ.Echo " -> " +
CStr(crible(i)) + " isn't a prime number: " + _
CStr(prime_number)
+ "x" + CStr(crible(i)/prime_number)
crible(i) = 0
multiple_found = true
ElseIf crible(i) <> prime_number And
crible(i) <> 0 And new_prime_number = 0 Then
new_prime_number = crible(i)
offset_prime = i
End If
Next
prime_number = new_prime_number '-- keep a new
prime number
If multiple_found = false Then
ScriptEZ.Echo
vbLf + "Prime numbers list beyond " +
CStr(localStorage + 2) + " is:
" + vbCrlf
For i=0 To localStorage
If crible(i) > 0 Then ScriptEZ.Echo " ." + CStr(crible(i))
Next
'-- Show API coverage
ScriptEZ.Echo vbLf + "View of API Coverage is:" + _
ScriptEZ.ConvertStringToBSTR(ScriptEZ.ProbeMemoryStatus(32))
ScriptEZ_OnRun = CLng(0) '-- done, the
search is achieved
Else
ScriptEZ_OnRun = CLng(1) '-- go ahead
again
End If
End Function