the function updates the richtextbox and loop works great but locks the GUI.
if i write the same function for generic System.Windows.Forms it all works as expected so i don't know if Powershell Studio handles it differently.
thanks for any guidance on this, driving me crazy for some time now
- $listbox1_SelectedIndexChanged = {
- $workstation= $listbox1.SelectedItem
- $script:timer = New-Object System.Windows.Forms.Timer
- $script:timer.Interval = 1000
- $script:timer.Add_Tick({
- CheckStatus
- })
- $script:timer.Start()
- }
- function CheckStatus
- {
- $job = Start-Job -ScriptBlock {
- $workstation = $Using:workstation
- Invoke-Command -computer *redacted* -ScriptBlock { Get-BrokerMachine -MaxRecordCount 2000 | ?{ $_.PublishedName -eq "$Using:workstation" } }
- }
- $job | wait-job | receive-job -outvariable status
- if ($status.registrationstate -eq 'Registered')
- {
- $richtextbox1.ForeColor = 'Green'
- $richtextbox1.Text = "$workstation is registered and should be available"
- }
- else
- {
- $richtextbox1.ForeColor = 'Red'
- $richtextbox1.Text = "$workstation is rebooting or not responding."
- }
- }
ERROR: You cannot call a method on a null-valued expression.
Power cycle WVM.psf (181, 1): ERROR: At Line: 181 char: 1
ERROR: + $script:timer.Stop()
ERROR: + ~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:
ERROR: You cannot call a method on a null-valued expression.