Powershell studio msgbox close freeze main 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 5 years and 4 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
GusFrings
Posts: 6
Last visit: Thu Dec 06, 2018 5:57 am

Powershell studio msgbox close freeze main form

Post by GusFrings »

Product, version and build: Powershell studio 2018

I create a form with a progress bar control set.
At the end I added an info msgbox.
Everything seems to work well, but I noticed that after I close the msgbox the main form remains "freezed" for a few seconds and you can not click on the buttons or even close with the x button.
After a few seconds instead you can go back to interacting with the main form.
Why does this happen? How can I solve?

Thank you

Code: Select all

$buttonCancelProcess_Click={
	$script:CancelLoop = $true
}

$buttonStartProcess_Click={
	#Init CancelLoop
	$script:CancelLoop = $false
	$buttonCancelProcess.Enabled = $true
	#Disable the button so we don't trigger it again
	$this.Enabled = $false
	#Reset the Progress Bar
	$progressbar1.Value = 0
	
	for($i = 0; $i -lt $progressbar1.Maximum; $i++)
	{
		#----------------------------------------
		#Place custom script here
		sleep -Milliseconds 200
		
		#----------------------------------------
		#process the pending message
		[System.Windows.Forms.Application]::DoEvents()
		
		if($script:CancelLoop -eq $true)
		{
			#Clear the progress bar
			$progressbar1.Value = 0
			#Exit the loop
			break;
		}
		#Step the progress bar
		$progressbar1.PerformStep() 
	}
	
	#Enable the button so we can click it again
	$this.Enabled = $true
	$buttonCancelProcess.Enabled = $false
	
	#msgbox
	$msg = '........'
	[void][System.Windows.Forms.MessageBox]::Show($msg, 'Info', 'OK', 'Exclamation')
	
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell studio msgbox close freeze main form

Post by jvierra »

Remove the DoEvents. It will cause issues when used repeatedly in a loop.
The code is just an experiment. You will not have this issue with real code designed correctly for a form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell studio msgbox close freeze main form

Post by jvierra »

You can also redo the logic to prevent the overrun you are seeing.

Code: Select all

$buttonStartProcess_Click={
    
    $script:CancelLoop = $false
    $buttonCancelProcess.Enabled = $true
    $this.Enabled = $false
    $progressbar1.Value = 0

    do{

        if($script:CancelLoop -eq $true){
            $progressbar1.Value = 0
            break;
        }
        $progressbar1.PerformStep()
        [System.Windows.Forms.Application]::DoEvents()
        sleep -Milliseconds 200

    }
    until($progressbar1.Value -eq $progressbar1.Maximum)

    $this.Enabled = $true
    $buttonCancelProcess.Enabled = $false

    $msg = '........'
    [void][System.Windows.Forms.MessageBox]::Show($msg, 'Info', 'OK', 'Exclamation')

}
GusFrings
Posts: 6
Last visit: Thu Dec 06, 2018 5:57 am

Re: Powershell studio msgbox close freeze main form

Post by GusFrings »

My code was generatedd from the toolbox control sets.
I tried to use your code but the behavior has not changed, so I get this error

Code: Select all

ERROR: +         $buttonCancelProcess.Enabled = $false
ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : PropertyNotFound
Thanks for your help
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell studio msgbox close freeze main form

Post by jvierra »

The error is on code that you posted. I just copied it. You must have broken something when copying the code.

See attached.
Attachments
Test-Stall.psf
(32.83 KiB) Downloaded 104 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell studio msgbox close freeze main form

Post by jvierra »

I placed a progress control set on a new form and ran it without making a single change and it works without delay. Something in your form must be causing an issue which is why I don't like DoEvents. It can cause events to be ignored and then posted out of order. If some other control is generating a lot of events then it can bean issue. Also changing the step and value will cause this code to have an issue due to the logic.
GusFrings
Posts: 6
Last visit: Thu Dec 06, 2018 5:57 am

Re: Powershell studio msgbox close freeze main form

Post by GusFrings »

Thank you for your help!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell studio msgbox close freeze main form

Post by jvierra »

Glad we got you going. 😊😎
This topic is 5 years and 4 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