Form stuck in minimized state while loop running?

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.
Post Reply
User avatar
raphaelgj
Posts: 8
Joined: Tue Jun 19, 2018 10:19 am

Form stuck in minimized state while loop running?

Post by raphaelgj » Fri Sep 21, 2018 9:19 am

This is driving me nuts :)

Simple proof of concept here, let's say I create an empty form with just one button:

Code: Select all

$form1_Load={
	
}

$button1_Click={
	do
	{
		[System.Windows.Forms.Application]::DoEvents()
		Start-Sleep -m 200
	}
	while (!$x)
}

That loop runs indefinitly and since i'm calling DoEvents at every iteration, the form stays responsive and i'm able to do anything with it.

Why then, when i MINIMIZE that form (using the _ button) , it can't be shown again? As if it's in a frozen minimized state?

I tried with refresh added in but still no luck, once it's minimized, there's no way to get it to show back up by clicking it's icon in the taskbar. What's going on?

Code: Select all

$button1_Click={
	do
	{
		$form1.Refresh()
		[System.Windows.Forms.Application]::DoEvents()
		Start-Sleep -m 200
	}
	while (!$x)
}
***If i double click the form from the Task Manager, it shows up, but not when clicking it's icon in taskbar...!

User avatar
mxtrinidad
Site Admin
Posts: 213
Joined: Sun Mar 03, 2013 12:42 pm

Re: Form stuck in minimized state while loop running?

Post by mxtrinidad » Fri Sep 21, 2018 9:52 am

What's the goal for this Proof-Of-Concept? What are you trying to make the form do?
Sorry! But, this is not clear enough.

Give us more detail!
:)

User avatar
raphaelgj
Posts: 8
Joined: Tue Jun 19, 2018 10:19 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj » Fri Sep 21, 2018 10:40 am

There is no need to do more, this simple code shows exactly the problem i'm facing, while my loop is running, once I minimize the window, it won't go to normal state again by clicking it in taskbar. Even if the loop did anything else than what is shown here, the behavior is the same. I don't want to complicate things by posting my whole application, it's huge and like I said this example shows the problem clearly.

User avatar
mxtrinidad
Site Admin
Posts: 213
Joined: Sun Mar 03, 2013 12:42 pm

Re: Form stuck in minimized state while loop running?

Post by mxtrinidad » Fri Sep 21, 2018 10:53 am

So basically .... the issue comes down to responsive vs non-responsive forms? Right!

User avatar
raphaelgj
Posts: 8
Joined: Tue Jun 19, 2018 10:19 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj » Fri Sep 21, 2018 10:56 am

Yes, my question is basically : why is the form responsive (I can click items fine or manipulate it) but once minimized, it's doomed to stay minimized unless called back via Task Manager? It's as if the click on taskbar event that restores the window is ignored or the window is restored but not visible. No idea why this happens?

User avatar
raphaelgj
Posts: 8
Joined: Tue Jun 19, 2018 10:19 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj » Fri Sep 21, 2018 11:19 am

Something even weirder I noticed, if I click away to some other window, when i click on the icon in the taskbar, it brings back the form properly. All while the loop is running. The problem is only when you minimize the window. Clicking the icon doesn't trigger the restore for some reason I don't understand.

User avatar
raphaelgj
Posts: 8
Joined: Tue Jun 19, 2018 10:19 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj » Fri Sep 21, 2018 11:26 am

Have a look at this demo, it shows exactly what's going on :
https://imgur.com/a/MOQlIZy

(click away and restoring is fine, but minimize it and it's gone for good. While the loop is NOT running, minimize works correctly)

User avatar
mxtrinidad
Site Admin
Posts: 213
Joined: Sun Mar 03, 2013 12:42 pm

Re: Form stuck in minimized state while loop running?

Post by mxtrinidad » Fri Sep 21, 2018 12:02 pm

That's a normal Windows Form behavior.

In order to make it responsive you need to look into the following articles that will help create responsive forms:
https://info.sapien.com/index.php/guis/ ... 5zaXZlIl0=
https://info.sapien.com/index.php/guis/ ... 5zaXZlIl0=
https://www.sapien.com/blog/2012/05/16/ ... ive-forms/

I think this above links can clear the issues you're experiencing.

:)

User avatar
jvierra
Posts: 12773
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Form stuck in minimized state while loop running?

Post by jvierra » Fri Sep 21, 2018 2:14 pm

I would also note that "DoEvents" can be useful it will also cause issues when run in a tight blocking loop.

Doing this is really a design failure and it should be avoided.

Designing with forms can be challenging and requires a good understanding of the Forms model and the event a behavior models. All Forms capabilities are designed to avoid loops. In compiled programs long running code is always handled ona new thread to avoid this.

As for the toolbar icon. The system handles this click event and will not change the Windo if the Window is already active which is why activating another app and then the icon works. To overcome this we would need to honor a different event in the form and code the behavior for this.

The bottom line is don't break rule 1. Don't use tight loops. "DoEvents" allows internal messages to be handled but ignores most system messages.

User avatar
raphaelgj
Posts: 8
Joined: Tue Jun 19, 2018 10:19 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj » Sat Sep 22, 2018 12:18 pm

Thank you for the clear information, much appreciated. Still very new to the form concepts...
The examples on the article mentioned are very complex :shock:
What I ended up doing, keeping it simple, is start a job, start a timer, and in the timer tick, update status bar after checking for a certain criteria. Works perfectly and form is finally 100% responsive, minimized or not :)

Thanks !

Post Reply