-ThrottleLimit
When starting a thread, the parameter -throttleLimit
sets the maximum number of concurrent running threads. This number can be upped and affects already started threads:
foreach ($t in 0..9) {
start-threadJob -scriptBlock {
param($x)
foreach ($i in 1 .. 10) {
write-host "thread $x is running (i = $i)"
start-sleep 1
}
} -streamingHost $host -throttleLimit 4 -argumentList $t
}
start-sleep 5
#
# start an 'empty thread' just to increase the throttle limit in order to
# run all threads concurrently:
#
$null = start-threadJob -scriptBlock {} -throttleLimit 11
Calling a function
This stackoverflow answer has a good summary that explains how a function can be called from a thread and why it needs to be done as shown below:
function func {
param([string] $arg)
$sleepDuration = get-random -maximum 5
start-sleep $sleepDuration
return "returning after $sleepDuration second(s), arg = $arg"
}
$threads = @()
foreach ($t in 'seven', 'eight-three', 'three', 'forty-eight', 'nine', 'one-hundred', 'twelve') {
$threads += start-threadJob {
param($arg)
$function:func = $using:function:func
func $arg
} -throttleLimit 10 -argumentList $t
}
$null = wait-job -job $threads
foreach ($thread in $threads) {
receive-job $thread
}
In order to use hyphenated function names, curly braces need to be used, like so:
function hyphenated-funcname {
param([string] $arg)
$sleepDuration = get-random -maximum 5
start-sleep $sleepDuration
return "returning after $sleepDuration second(s), arg = $arg"
}
$threads = @()
foreach ($t in 'seven', 'eight-three', 'three', 'forty-eight', 'nine', 'one-hundred', 'twelve') {
$threads += start-threadJob {
param($arg)
${function:func-alias} = ${using:function:hyphenated-funcname} # <<< Note the curly braces here.
func-alias $arg
} -throttleLimit 10 -argumentList $t
}
‥
-StreamingHost
By default, the thread's output is collected into the job data stream and won't be shown in the host (unless explicitely requested with receive-job
)
With -StreamingHost
, this output can be sent to the specified host:
foreach ($t in 1..10) {
start-threadJob -scriptBlock {
param($x)
write-host "thread $x is going to sleep"
start-sleep $x
write-host "thread $x has woken up"
} -streamingHost $host -argumentList $t
}