token: > this thread hurts my head
I'd say that a "thread" about "multi-threading" branching into multiple sub-threads sounds somehow appropriate. It's good conceptual practice. =)
So anyway, I came up with a contrived example of multi threading, which is basically a main script running several sub scripts. The main script then watches a text file to wait until all of the sub scripts have finished.
I then went and looked at that ThreadForker routine and it seems to be a similar kind of thing, but with the capability of killing sub processes that run for too long.
However, my example is smaller and simpler and demonstrates multiple scripts reading and writing the same text file, so I'm posting it for everyone's amusement.
'- MainScript.vbs
' Todd Fiske
' 2005-03-19 00:11:19
'- start several instances of SubScript each in their own minimized window
'- each subscript will be passed a random number which basically means how
' many times to execute its main loop
' the loop will just sleep for a random amount of time then write a line to
' an output text file
' when done looping, the subscript will write a "done" line to a tracking
' text file
'- after starting the instances of SubScript, MainScript will run a loop that
' reads the tracking text file and continues until all of the subscripts
' report that they are done
Option Explicit
Randomize
dim fso, shl
set fso = CreateObject("Scripting.FileSystemObject")
set shl = CreateObject("WScript.Shell")
dim nSubScripts, nScript, nArg
dim aSubScripts()
dim sCmd, nRet, bDone, bRunning
dim f, sFile, aFile, sLine, nNum, bFlag, nLoop
dim sTrackingFile, sOutputFile
sTrackingFile = "MultiScripts.txt"
sOutputFile = "MultiOutput.txt"
'- choose number of subscripts to run
nSubScripts = ( Rnd * 10 ) + 5 '- run between 5 and 14 sub scripts
say "creating " & nSubScripts & " sub scripts"
redim aSubScripts(nSubScripts)
'- delete tracking and output files
on error resume next
fso.DeleteFile(sTrackingFile)
fso.DeleteFile(sOutputFile)
on error goto 0
'- launch subscripts
nScript = 1
do while nScript <= nSubScripts
nArg = (rnd * 20 ) + 10 '- 10 to 29
sCmd = "cscript SubScript.vbs " & nScript & " " & nArg
nRet = shl.Run(sCmd, 2, false) '- run minimized, don't wait
aSubScripts(nScript) = true '- set Running flag
say sCmd & " -> " & nRet
nScript = nScript + 1
loop
'- loop until all subscripts are done
bDone = false
nLoop = 1
say "looping until all sub-scripts are done"
do until bDone
WScript.Sleep 100
'- load file into string
if fso.FileExists(sTrackingFile) then
set f = fso.OpenTextFile(sTrackingFile, 1) '- for reading
sFile = f.ReadAll
f.Close
'- split into array
sFile = Left(sFile, len(sFile) - 2) '- trim final CRLF
aFile = Split(sFile, vbCrLf)
'- turn off any flags for sub scripts that have finished
for each sLine in aFile
nNum = Trim(Mid(sLine,11,2)) '- extract number "SubScript ## done"
if aSubScripts(nNum) then
say sLine
aSubScripts(nNum) = false '- set that item of aSubScripts to false
end if
next
'- see if anyone is still running
bRunning = false
for each bFlag in aSubScripts
bRunning = bRunning or bFlag
next
bDone = not bRunning
end if
nLoop = nLoop + 1
loop
say "done after " & nLoop & " loops"
'---
sub Say(s)
WScript.Echo s
end sub
'===
'- SubScript.vbs
Option Explicit
Randomize
dim nID, nArg, nLoop, nSleep, sLine
dim fso, f
set fso = CreateObject("Scripting.FileSystemObject")
nID = WScript.Arguments(0)
nArg = Int(WScript.Arguments(1))
'~ could get filenames as parameters too
WScript.Echo "running SubScript " & nID & " " & nArg
WScript.Echo "looping " & nArg & " times"
nLoop = 0
do while nLoop < nArg
nSleep = (rnd * 5) + 3 '- 3 to 7
WScript.Sleep nSleep * 1000
sLine = "SubScript " & nID & " slept for " & nSleep & " seconds on loop " & nLoop & " of " & nArg
WScript.Echo sLine
set f = fso.OpenTextFile("MultiOutput.txt", 8, true) 'i for appending, can create
f.WriteLine sLine
f.Close
nLoop = nLoop + 1
loop
WScript.Echo "done SubScript " & nID & " " & nArg
set f = fso.OpenTextFile("MultiScripts.txt", 8, true) 'i for appending, can create
f.WriteLine "SubScript " & nID & " done"
f.Close
'=== I'm going camping this weekend, so if I don't reply its because I'm trading computers for woods and snow for a little while. Will be back Tuesday.
-toddio