trouble with responsive form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 9 months and 4 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
monderick
Posts: 7
Last visit: Thu Jan 18, 2024 7:44 am
Answers: 1
Has voted: 1 time

trouble with responsive form

Post by monderick »

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
  1. $listbox1_SelectedIndexChanged = {
  2.     $workstation= $listbox1.SelectedItem
  3.     $script:timer = New-Object System.Windows.Forms.Timer
  4.     $script:timer.Interval = 1000
  5.     $script:timer.Add_Tick({
  6.             CheckStatus
  7.         })
  8.     $script:timer.Start()
  9. }
  10.  
  11. function CheckStatus
  12. {
  13.     $job = Start-Job -ScriptBlock {
  14.         $workstation = $Using:workstation
  15.         Invoke-Command -computer *redacted* -ScriptBlock { Get-BrokerMachine -MaxRecordCount 2000 | ?{ $_.PublishedName -eq "$Using:workstation" } }
  16.     }
  17.         $job | wait-job | receive-job -outvariable status
  18.        
  19.     if ($status.registrationstate -eq 'Registered')
  20.          {
  21.         $richtextbox1.ForeColor = 'Green'
  22.         $richtextbox1.Text = "$workstation is registered and should be available"
  23.         }
  24.     else
  25.     {
  26.         $richtextbox1.ForeColor = 'Red'
  27.         $richtextbox1.Text = "$workstation is rebooting or not responding."
  28.     }
  29.  
  30. }
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.
This topic is 9 months and 4 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked