I'd like for the console to trigger functions when a job has new data (from a job started with Start-ThreadJob
), i.e. whenever .HasMoreData
is True. I was hoping Register-ObjectEvent would help me in this regard, but unfortunately it seems that there's no event for HasMoreData. There is an event for the job's state changing though.
One solution might be to have the job's scriptblock change the state of the job whenever more data is expected, though I'm not sure the best way to do that.
The best solution would probably be to override some part of the ThreadJob class to give it an event related to HasMoreData that can be registered, but that's currently beyond my ability.
Your question is lacking clarity as to what exactly you want to do on data adding but as an example, instead of using ThreadJobs you can use powershell
instances directly, if you invoke them async using one of the BeginInvoke<TInput, TOutput>
overload you can hook a PSDataCollection<T>
and register to its DataAdding
event:
try {
$rs = [runspacefactory]::CreateRunspace()
$rs.Open()
$ps = [powershell]::Create().AddScript({
foreach ($i in 0..30) {
Get-Random
Start-Sleep 1
}
}, $true)
$ps.Runspace = $rs
$output = [System.Management.Automation.PSDataCollection[psobject]]::new()
$iasync = $ps.BeginInvoke(
[System.Management.Automation.PSDataCollection[psobject]] $null,
$output)
$registerObjectEventSplat = @{
InputObject = $output
EventName = 'DataAdding'
SourceIdentifier = 'Testing'
}
$null = Register-ObjectEvent @registerObjectEventSplat {
param($s, [System.Management.Automation.DataAddingEventArgs] $e)
Write-Host "$([datetime]::Now.ToString('s')) - An item was added: '$($e.ItemAdded)'"
}
while (-not $iasync.AsyncWaitHandle.WaitOne(200)) { }
}
finally {
Unregister-Event Testing
$output.Dispose()
$ps.Dispose()
$rs.Dispose()
}