WinWaitCreated() - Wait for a new window

Post your working scripts, libraries and tools for AHK v1.1 and older
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: WinWaitCreated() - Wait for a new window

01 Mar 2016, 19:50

5ball wrote:.. and sometimes open more windows than requested.
This function does not open windows!
Which AHK_H version are you using, do you have some example code.
5ball
Posts: 12
Joined: 28 Feb 2016, 10:04

Re: WinWaitCreated() - Wait for a new window

02 Mar 2016, 04:26

Yes, sorry, I explained that poorly.
Here is an example code (warning: this script will open 100 instances of Notepad.exe!).


Downloaded both new AHK_L and H files yesterday so I'm using most recent version of AHK_L and I downloaded AHK_H from https://github.com/HotKeyIt/ahkdll-v1-release, I assume this is the most recent release.
To use AHK_H I've dumped the contents of the Win32w file into my ProgramFiles/AutoHotKey folder. Is this the correct way to go about that?


With AHK_L this works seemingly flawlessly. It will virtually always get to 100 open (I don't think I've experienced it missing windows yet, aside from when I rapidly "X" out windows as they are being spawned).
With AHK_H, this script works occasionally, but very often it will only get 4-12 open before it misses a new window opening and then it hangs (as it is waiting for a new instance to be created).

My actual program doesn't need to open notepad, it clicks on a control inside of another program and waits for a new window to spawn - this also works perfectly with AHK_L but I'd like to use AHK_H for some other features of the program.

a GUI opened windows counter included

Edit: even adding a "Sleep, 500" line in there, I am still having issues with it occasionally hanging up after a new window opens

Code: Select all

Gui, Add, Text, vCO w120 center, %CurrentlyOpen%
Gui, show, w150 h30

KeepOpen = 100
CurrentlyOpen = 0

Loop
{
	WinGet, CurrentlyOpen, Count, Untitled - Notepad
	GuiControl, , CO, %CurrentlyOpen%
	If (CurrentlyOpen < KeepOpen)
	{
		Run notepad.exe
		WinWaitCreated("Untitled - Notepad")
	}
}
return

GuiClose:
exitapp
return	
/*
  Wait for a window to be created, returns 0 on timeout and ahk_id 

otherwise
  Parameter are the same as WinWait, see 

http://ahkscript.org/docs/commands/WinWait.htm
  Forum: http://ahkscript.org/boards/viewtopic.php?

f=6&t=1274&p=8517#p8517
*/
WinWaitCreated( WinTitle:="", WinText:="", Seconds:=0, 

ExcludeTitle:="", ExcludeText:="" ) {
    ; HotKeyIt - http://ahkscript.org/boards/viewtopic.php?t=1274
    static Found := 0, _WinTitle, _WinText, _ExcludeTitle, _ExcludeText 
         , init := DllCall( "RegisterShellHookWindow", "UInt",A_ScriptHwnd )
         , MsgNum := DllCall( "RegisterWindowMessage", 

"Str","SHELLHOOK" )
         , cleanup:={base:{__Delete:"WinWaitCreated"}}
  If IsObject(WinTitle)   ; cleanup
    return DllCall("DeregisterShellHookWindow","PTR",A_ScriptHwnd)
  else if (Seconds <> MsgNum){ ; User called the function
    Start := A_TickCount, _WinTitle := WinTitle, _WinText := WinText
    ,_ExcludeTitle := ExcludeTitle, _ExcludeText := ExcludeText
    ,OnMessage( MsgNum, A_ThisFunc ),  Found := 0
    While ( !Found && ( !Seconds || Seconds * 1000 < A_TickCount - 

Start ) ) 
      Sleep 16                                                         
    Return Found,OnMessage( MsgNum, "" )
  }
  If ( WinTitle = 1   ; window created, check if it is our window
    && ExcludeTitle = A_ScriptHwnd
    && WinExist( _WinTitle " ahk_id " 

WinText,_WinText,_ExcludeTitle,_ExcludeText))
    WinWait % "ahk_id " Found := WinText ; wait for window to be shown
}
Thank you so much for your help with this, HotKeyIt. I can't even tell you how many times your posts have saved my butt :)
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: WinWaitCreated() - Wait for a new window

03 Mar 2016, 09:53

The problem is that the window is created before we start waiting for it!

Code: Select all

SetBatchLines, -1
Gui, +AlwaysOnTop
Gui, Add, Text, vCO w120 center, %CurrentlyOpen%
Gui, show, w150 h30
 
KeepOpen = 100
CurrentlyOpen = 0
 
Loop
{
	WinGet, CurrentlyOpen, Count, Untitled - Notepad
	GuiControl, , CO, %CurrentlyOpen%
	If (CurrentlyOpen < KeepOpen)
	{
		DynaRun("Sleep 10`nRun notepad.exe") ; run notepad in a different process so WinWaitCreated has a chance to receive the message!
		WinWaitCreated("Untitled - Notepad")
	}
	else break
}
return
 
GuiClose:
Loop {
	Process,Exist,notepad.exe
	If !Errorlevel
		break
	Process, Close, %ErrorLevel%
}
ExitApp
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: WinWaitCreated() - Wait for a new window

04 Mar 2016, 07:53

Are there any techniques in this code that could be used to detect change of active window without needing a WinExist("A") loop?
As part of UCR, at some point I will need code so that a class property always holds the title of the currently active window.

I am guessing if you had a WinExist loop with a sleep 10 in it, the class property could potentially be inaccurate for 9ms.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: WinWaitCreated() - Wait for a new window

04 Mar 2016, 09:08

evilC wrote:Are there any techniques in this code that could be used to detect change of active window without needing a WinExist("A") loop?
yes

https://autohotkey.com/board/topic/8064 ... -messages/

HotKeyIt is simply monitoring for HSHELL_WINDOWCREATED. Just look for HSHELL_WINDOWACTIVATED instead

https://msdn.microsoft.com/en-us/library/ms644989.aspx

5ball
Posts: 12
Joined: 28 Feb 2016, 10:04

Re: WinWaitCreated() - Wait for a new window

07 Mar 2016, 15:09

HotKeyIt wrote:The problem is that the window is created before we start waiting for it!
I figured that might be the issue, and it turns out it isn't a problem for my application.
I wasn't even aware of dynarun, that is awesome.

I can't thank you enough, HotKeyIt. You really are an amazing contributor.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: WinWaitCreated() - Wait for a new window

16 Apr 2016, 16:57

Just spent a good chunk of time solving this exact problem yesterday... your code looks much nicer :thumbup: --I'll try it out.

Thanks for posting... wish I would have found it sooner.
User avatar
niczoom
Posts: 78
Joined: 09 Mar 2016, 22:17

Re: WinWaitCreated() - Wait for a new window

24 Oct 2016, 22:16

Thankyou very much for this code.

It helped me dealing with newly created 'Explorer' windows.
[AHK] 1.1.23.05 x32 Unicode
[WIN] 10 Pro x64 Version 5111 (Build 10586.218)
l4d3
Posts: 10
Joined: 17 Mar 2021, 02:15

Re: WinWaitCreated() - Wait for a new window

07 Jul 2021, 08:59

Is there any way extend this to use window handle (HWND) instead? The software GIMP for instance has all of its spawned windows at same properties so this function can't detect new windows inside GIMP. This probably is the case for other similar softwares as well.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: WinWaitCreated() - Wait for a new window

07 Jul 2021, 15:17

The windows inside GIMP are like Controls, so you will rather need a ControlWaitCreated function!
l4d3
Posts: 10
Joined: 17 Mar 2021, 02:15

Re: WinWaitCreated() - Wait for a new window

08 Jul 2021, 04:36

HotKeyIt wrote:
07 Jul 2021, 15:17
The windows inside GIMP are like Controls, so you will rather need a ControlWaitCreated function!
Do you know what I should change in the function to make it detect newly created controls?
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: WinWaitCreated() - Wait for a new window

08 Jul 2021, 11:11

@l4d3 I was wondering if maybe what you are trying to do is more simple?
Have you tried this? (but make sure that your exe is called the same)

Code: Select all

Hwnd := WinWaitCreated("ahk_exe gimp-2.10.exe")
This can work for example to find Hwnd of the window that appears when you click the menu File > New...

EDIT:
Since there wasn't an immediate response and I don't want to leave the thread hanging like this,
my suggestion implies a possible confusion between process and window.
If my suggestion was enough, then what the user needed to pass was not an Hwnd (window id) but some way of identifying the process.
In this case, I didn't use a process Id, but the program name because I thought it was enough for the task.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: nitsik and 168 guests