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.
This topic is 2 years and 10 months 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
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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
This topic is 2 years and 10 months 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