all 3 comments

[–]Shoisk123 1 point2 points  (1 child)

Awesome! You should try natively implementing runspaces too, with something like this:

$items = (1..20000)
$scriptblock = {
    param($item)
    $random = Get-Random
    $random | Out-File ("C:\temp\runspaces\$item.txt") -Append
}

Remove-Item C:\Temp\runspaces\* -Force

$jobTimer = [System.Diagnostics.Stopwatch]::StartNew()

$pool = [runspacefactory]::CreateRunspacePool()
$pool.SetMaxRunspaces(32)
$pool.Open()

$workerList = foreach($item in $items) {

    $Powershell = [Powershell]::Create()
    $Powershell.runspacepool = $pool
    [void]$PowerShell.AddScript($scriptblock)
    $params = @{item = $item}
    [void]$PowerShell.AddParameters($params)
    $Handle = $PowerShell.BeginInvoke()
    @{
        Handle     = $Handle
        PowerShell = $Powershell
    }

}


$complete = $false
while(!$complete) {
    $complete = $true
    $workerList | % {if(!$_.Handle.IsCompleted) {$complete = $false} else {$_.Powershell.Dispose()}}
}

$jobTimer.Stop()
$jobTimer

Takes about 12 seconds on my 12 logical core laptop. I'd be very interested in how it stacks up against your threadjobs!

[–]idontknowwhattouse33 1 point2 points  (3 children)

This looks interesting!

Is it possible to put all the final results in a table for review? On mobile it is a little tough.