i went through the 'creating responsive forms' blog post and have a 99% working script but cannot find where to insert the $script:timer.Stop() to reset the loop in the below listbox snippet.
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
if i just put $script:timer.Stop() at the end of listbox1_SelectedIndexChanged block, it errors:
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.
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
Code:
$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.
Statistics: Posted by monderick — Mon Jan 15, 2024 8:39 am