Prevent Script from Freezing PS Studio 2019 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.
Locked
User avatar
mar10c
Posts: 21
Last visit: Fri Jun 11, 2021 12:36 pm

Prevent Script from Freezing PS Studio 2019 form

Post by mar10c »

I am trying to prevent the form from freezing/unresponsive when the $output can't connect to remote computer. I need to set the timer to release the event back to the form.

$button1.Enabled = $false
$TotalTime = 10 #in seconds
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Start the timer
$timerUpdate.Start()
[TimeSpan]$span = $script:StartTime - (Get-Date)
if ($span.TotalSeconds -le 0)
{
$timerUpdate.Stop()
}
$output = ssh user1@$remotecomputer

$button1.Enabled = $true

jvierra
Posts: 14867
Last visit: Fri Jul 23, 2021 7:52 pm
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: Prevent Script from Freezing PS Studio 2019 form

Post by jvierra »

Events cannot be terminated. If a call blocks in an event you will just have to wait for completion or for a timeout. If it deadlocks then it is likely due to some system failure although third party software may not be well designed and may not prevent a complet4e freeze.

You can run scripts in a job and then kill the job if it hangs.

See: https://info.sapien.com/index.php/guis/ ... sive-forms

Locked