Skip to content

Instantly share code, notes, and snippets.

@jrotello
Last active November 2, 2016 16:10
Show Gist options
  • Save jrotello/2e42a42f5a40c446708b330c5d7f461f to your computer and use it in GitHub Desktop.
Save jrotello/2e42a42f5a40c446708b330c5d7f461f to your computer and use it in GitHub Desktop.
PowerShell Jobs Example
param(
$MaxJobs = $env:NUMBER_OF_PROCESSORS
)
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
$jobs = @()
1..25 | % {
$running = Get-Job -State Running
if (($running | Measure-Object).Count -ge $MaxJobs) {
"Waiting..."
Wait-Job -Job $running -Any | Out-Null
}
$i = $_
"Creating job #$($i)"
$jobs += Start-Job {
Start-Sleep -Seconds 3
"Finished job #$($using:i)"
}
}
"Waiting for jobs to complete..."
Wait-Job $jobs | Out-Null
"Receiving jobs..."
$jobs | Receive-Job
"Removing jobs..."
$jobs | Remove-Job
"Done. $($elapsed.ElapsedMilliseconds)ms"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment