If two background jobs are started from the same session, how can one of them determine that the other has finished? Initially I tried something like this:
$j1 = Start-Job {
Start-Sleep -Seconds 5
"j1 done"
}
$j2 = Start-Job {
param($j)
"j2 waiting..."
$j | Wait-Job
"j2 done"
} -ArgumentList $j1.Finished
$j1,$j2 | Receive-Job -Wait
Of course it doesn't work because $j2
only gets a serialized snapshot of $j1
(the job Status in the snapshot would never change); furthermore each background job has their own job repository so $j
object appears to be bogus in the context of $j2
.
The jobs can synchronize via the mutexes, filesystem (if on the same machine) or a DB, etc. but I'm wondering if powershell provides any remoting-friendly facilities for this?