Page 1 of 1

Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 4:12 am
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')
	
}

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 4:23 am
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.

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 4:34 am
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')

}

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 5:28 am
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

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 5:35 am
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.

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 5:52 am
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.

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 6:41 am
by GusFrings
Thank you for your help!

Re: Powershell studio msgbox close freeze main form

Posted: Thu Nov 15, 2018 7:17 am
by jvierra
Glad we got you going. 😊😎