Powershell: Updating GUI from background job results Powershell: Updating GUI from background job results powershell powershell

Powershell: Updating GUI from background job results


You can use events:

 $job = Start-Job {sleep 3; Write-Output "Dummy job completed"}$callback = {Write-Host "Event Fired"Unregister-Event -SourceIdentifier "DummyJob"Write-Host ($job | Receive-Job)}$evt = Register-ObjectEvent -InputObject $job -EventName StateChanged -SourceIdentifier "DummyJob" -Action $callback # to remove all Get-Job | Remove-Job -Force # jobs and eventsGet-EventSubscriber | Unregister-Event  # events


You might want to post this on CodeReview.StackExchange.com.

I sort of hate it when people build UI's in Powershell. If you want a proper Windows forms app, just write it in C#. So I disagree with the design at its premise.

I like your impulse to move away from the polling design; you initiate the job then poll to see if it's completed. I think an event handler might be a better choice. Check out the section "Monitor a Background Job" in the article PowerShell and Events: Object Events. It's an oldie but a goodie.