need clarity on winwaitnotactive

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

need clarity on winwaitnotactive

Post by newcod3r » 05 Jul 2022, 04:17

It seems I'm not using this incorrectly but I'm not sure how to correct it.

"Updating AI Masks" is a pop-up dialog box that processes the photo and once done, it will dismiss by itself.
The process can take anywhere from 2 seconds to 2 minutes.
AgWinMainFrame is the main window.
I want the actions below to occur only processing is done. What is wrong about the code below?

Separately, is there an easier way to debug code that involves loops? Thinking of a less intrusive way to show issues rather than msgboxes/tooltips.

Code: Select all

Loop
{
    WinWaitNotActive, Updating AI Masks,, 2
} until (ErrorLevel = 0)
Sleep 200
If WinExist("ahk_class AgWinMainFrame") {
Sleep 300
SendInput ^+d ;deselect photos
Sleep 100
SendInput {Right} ;move to next photo 
}

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 05 Jul 2022, 04:59

WinWait can wait indefinitely, so you would not need a loop for that (and would not want one).

Code: Select all

dialog = Updating AI Masks
; dialog = ahk_exe notepad.exe
main   = ahk_class AgWinMainFrame
; main   = ahk_exe firefox.exe
WinWait, %dialog%
WinWaitClose
If WinExist(main)
     MsgBox, 64, Success, Exists!
Else MsgBox, 48, Success, Failure!

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: need clarity on winwaitnotactive

Post by newcod3r » 05 Jul 2022, 17:56

may I ask if winwaitclose continually checks whether the window is closed? if yes, then the issue with my script probably lies elsewhere.. 😅

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 05 Jul 2022, 18:23

I imagine it's a hook-style process-- a state of waiting-- rather than an active loop, but will need some experts to weigh in on that. You could try the script to see if it works.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: need clarity on winwaitnotactive

Post by boiler » 05 Jul 2022, 19:38

I believe it's actually a loop that checks to see if the window still exists based on what I can see in the AHK source code. This from WinClose() says that it's the same for WinWaitClose(), which is that it waits the specified amount of time (which can be no end time if no timeout is specified) before breaking the loop if it hasn't already broken the loop because the window was found to no longer exist:

Code: Select all

	// This is the same basic code used for ACT_WINWAITCLOSE and such:
	for (;;)
	{
		// Seems best to always do the first one regardless of the value 
		// of aTimeToWaitForClose:
		MsgSleep(INTERVAL_UNSPECIFIED);
		if (!IsWindow(aWnd)) // It's gone, so we're done.
			return aWnd;
		// Must cast to int or any negative result will be lost due to DWORD type:
		if ((int)(aTimeToWaitForClose - (GetTickCount() - start_time)) <= SLEEP_INTERVAL_HALF)
			break;
			// Last param 0 because we don't want it to restore the
			// current active window after the time expires (in case
			// it's suspended).  INTERVAL_UNSPECIFIED performs better.
	}
	return aWnd;  // Done waiting.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 05 Jul 2022, 19:52

Thank you, boiler! 8-)

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: need clarity on winwaitnotactive

Post by newcod3r » 05 Jul 2022, 20:09

Thanks both. If it is indeed a loop by itself, then it should work, but I can't think of another reason why ^+d is not sent, especially if the "updating" process takes a few minutes (rather than <10s).

And yes I did confirm that the main class is activated when the update process is done.. so it should flow down accordingly.

Code: Select all

If WinExist("Updating AI Masks") {
WinWait, Updating AI Masks
WinWaitClose
}
If WinExist("ahk_class AgWinMainFrame") {
sleep 100
SendInput ^+d ;deselect photos
Sleep 150
SendInput ^{Right} ;move to next photo 
}

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 05 Jul 2022, 20:11

Try my script before changing it. You do not need If WinExist (line 1) because WinWait, well, it waits for a window (to exist). :) Three problems with your changes are (1) if the window exists, then you don't need to wait for it to exist; (2) if the window does not exist, then the block is skipped instead of waiting for the window; and (3) with your change, you don't really know whether your second conditional statement is met.

My script avoids all three problems. If my script works as is, then you have a working basis upon which to expand.

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: need clarity on winwaitnotactive

Post by newcod3r » 05 Jul 2022, 21:02

mikeyww wrote:
05 Jul 2022, 20:11
Try my script before changing it. You do not need If WinExist (line 1) because WinWait, well, it waits for a window (to exist). :) Three problems with your changes are (1) if the window exists, then you don't need to wait for it to exist; (2) if the window does not exist, then the block is skipped instead of waiting for the window; and (3) with your change, you don't really know whether your second conditional statement is met.

My script avoids all three problems. If my script works as is, then you have a working basis upon which to expand.
Hi! It is structured this way because the "Updating AI Mask" process is not always being run. If it is not running, then I don't want to do a "WinWait" for the process, else it will just get stuck waiting for a process which will never run (it just goes back to the main ahk_class).

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 05 Jul 2022, 21:21

You posted a question. I posted an answer. If you don't want to give it a whirl, then others here may have additional answers for you! Cheers.

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: need clarity on winwaitnotactive

Post by newcod3r » 05 Jul 2022, 21:29

mikeyww wrote:
05 Jul 2022, 21:21
You posted a question. I posted an answer. If you don't want to give it a whirl, then others here may have additional answers for you! Cheers.
Hi Mike, please don't get me wrong.. I was just trying to explain what I'm trying to achieve. I have tried your style too (removed the ;If WinExist("Updating AI Masks")), and the same issue persists.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 05 Jul 2022, 21:41

So you ran my exact script and.... what happened? Either one of the MsgBox appeared, or the script waited forever. Which was it?

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: need clarity on winwaitnotactive

Post by boiler » 05 Jul 2022, 22:42

newcod3r wrote: may I ask if winwaitclose continually checks whether the window is closed? if yes, then the issue with my script probably lies elsewhere.. 😅
The answer as to how AHK handles WinWaitClose internally doesn't matter at all to your script. You're not thinking correctly about how it works by thinking it would. Either way, the effect is the same other than possible CPU usage. mikeyww is right that there is no reason whatsoever to put a WinWait-type of command inside a loop and keep checking it. Just use the command by itself either with a timeout or without. Your attempt at putting it into a loop is doing nothing the command can't do on its own.

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: need clarity on winwaitnotactive

Post by newcod3r » 05 Jul 2022, 22:58

mikeyww wrote:
05 Jul 2022, 21:41
So you ran my exact script and.... what happened? Either one of the MsgBox appeared, or the script waited forever. Which was it?
in your exact script, it works, but when applying to Lightroom (when there's a variable of wait time between 10s - 5mins+ for processing), it doesn't execute the remaining code when returning to main window.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: need clarity on winwaitnotactive

Post by mikeyww » 06 Jul 2022, 05:56

If my script waits forever, then it means one of two things:
1. The dialog window never exists.
2. The dialog window is never closed.

If you are seeing the window open and close, then it means that you have specified the wrong WinTitle, or the window is hidden. You can run Window Spy to check your WinTitle. The current WinTitle in the script is Updating AI Masks. If needed, you can post your revised test script below.

Post Reply

Return to “Ask for Help (v1)”