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.
This topic is 5 years and 6 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
raphaelgj
Posts: 23
Last visit: Thu Aug 17, 2023 8:41 am

Form stuck in minimized state while loop running?

Post by raphaelgj »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Form stuck in minimized state while loop running?

Post by mxtrinidad »

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: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Form stuck in minimized state while loop running?

Post by mxtrinidad »

So basically .... the issue comes down to responsive vs non-responsive forms? Right!
User avatar
raphaelgj
Posts: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj »

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: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj »

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: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj »

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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Form stuck in minimized state while loop running?

Post by mxtrinidad »

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.

:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form stuck in minimized state while loop running?

Post by jvierra »

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: 23
Last visit: Thu Aug 17, 2023 8:41 am

Re: Form stuck in minimized state while loop running?

Post by raphaelgj »

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 !
This topic is 5 years and 6 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