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.
Locked
GusFrings
Posts: 6
Joined: Thu Nov 15, 2018 3:24 am

Powershell studio msgbox close freeze main form

Post by GusFrings » Thu Nov 15, 2018 4:12 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell studio msgbox close freeze main form

Post by jvierra » Thu Nov 15, 2018 4:23 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell studio msgbox close freeze main form

Post by jvierra » Thu Nov 15, 2018 4:34 am

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
Joined: Thu Nov 15, 2018 3:24 am

Re: Powershell studio msgbox close freeze main form

Post by GusFrings » Thu Nov 15, 2018 5:28 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell studio msgbox close freeze main form

Post by jvierra » Thu Nov 15, 2018 5:35 am

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 31 times

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell studio msgbox close freeze main form

Post by jvierra » Thu Nov 15, 2018 5:52 am

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
Joined: Thu Nov 15, 2018 3:24 am

Re: Powershell studio msgbox close freeze main form

Post by GusFrings » Thu Nov 15, 2018 6:41 am

Thank you for your help!

jvierra
Posts: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell studio msgbox close freeze main form

Post by jvierra » Thu Nov 15, 2018 7:17 am

Glad we got you going. 😊😎

Locked