Search notes:

PowerShell cmdLet Start-ThreadJob

-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
}

See also

Filling Excel worksheets simultaneously with multiple threads.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:51 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(51): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(66): id_of(Object(PDO), 'uri', '/notes/Windows/...') #2 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1759390690, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #3 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/command-inventory/noun/threadJob/start/index(127): insert_webrequest() #4 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 51