GetWindowHandle Timeout?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

GetWindowHandle Timeout?

Post by Phil Pendlebury » Sat Jul 09, 2022 1:32 pm

Well, the forum is quiet at the moment so why not keep posting:

In my code I have a number of "GetWindowHandle>" commands and if one fails the next one takes over etc.

However, because there is a number of different windows to search for I would like to set the time spent searching much lower than the default. The default seems to be about 5-6 seconds. So if I am searching a list of say 5 windows, and the actual window open is the last one on the list, this can take 30 seconds...

And sometimes none of the windows exist so I then display message.

What I am looking for is something like: GWH_TIMEOUT so I can set

Code: Select all

Let>GWH_TIMEOUT=1
But of course this command doesn't exist. Or does it? If it does I can not find it anywhere.

Meanwhile I guess I can combine the search with IfNotWindowOpen or IfWindowOpen - Nope no timeout on those either.

OK let's try WaitWindowOpen - Nope this has a timeout but no IF.

Also tried using GetWindowList, thinking I can just search a list of open windows before trying to get their handle but strangely the window that does exist and shows in MS's "View System Windows" does not exist in GetWindowList output.
Phil Pendlebury - Linktree

User avatar
Grovkillen
Automation Wizard
Posts: 808
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: GetWindowHandle Timeout?

Post by Grovkillen » Sun Jul 10, 2022 5:18 am

I would think that if you do this, lowering the time, you may get false negative results. I can imagine that the command will start parsing from the top for each iterating. Why not use RegEx instead?

Let>WIN_REGEX=1
Let>ME=%Script%

Running: 15.0.20
version history

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: GetWindowHandle Timeout?

Post by Phil Pendlebury » Sun Jul 10, 2022 6:50 am

I am using regex already. :-)

This is not the point tbh.
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: GetWindowHandle Timeout?

Post by Phil Pendlebury » Sun Jul 10, 2022 11:56 am

I am sorry for me previously brief answer. I was trying to type on phone.

I am using Regex to search for the Windows. (See here): viewtopic.php?f=2&t=11182

There a re few issues:
  • There is only ever 1 window with the relevant name open and it does not appear in get Window List. (So I cannot check it with that).
  • So I search for the first one, (using GetWindowHandle) and if handle is still zero, move on to search for the next one etc.
  • This is not too bad when 1 of the windows exists (which it usually does).
  • But in the case of where the user has opened the app before opening the one of the applications that I search for, my app completely hangs while trying (and failing) to find one of relevant windows, which of course does not exist).
I have got around this for now by making my app NOT check for windows and then opening to a menu screen which prompts the user to open one of the apps and then scan for the window.

But a simple timoput feature would solve this completely.
Phil Pendlebury - Linktree

User avatar
Grovkillen
Automation Wizard
Posts: 808
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: GetWindowHandle Timeout?

Post by Grovkillen » Sun Jul 10, 2022 4:44 pm

Use a custom event which look for the window continously. The custom event isn't part of the main loop. You can then keep track of a variable which is set to 1 or 0. If it's 0 just go into a loop in the main thread for X amount of iterations just to be sure you wait a minimum time before acting.
Let>ME=%Script%

Running: 15.0.20
version history

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: GetWindowHandle Timeout?

Post by Phil Pendlebury » Sun Jul 10, 2022 10:07 pm

Thank you sir. I will look up custom events. I have a vague memory of using this method sometime many years ago.

Thanks again for taking the time to read through my story.

I’ll post back…
Phil Pendlebury - Linktree

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: GetWindowHandle Timeout?

Post by Phil Pendlebury » Tue Jul 12, 2022 7:20 am

OK so after a brief search of the forum for how it works, I borrowed some code and adjusted it slightly to make a rough code that demos how this works:

It will show a counter in message form every .5 seconds.
Meanwhile trying to find the handle of a (notepad++) window named "new 4 (any other text)"
As soon as the window handle is not zero, the trigger will show a modal message saying "Found it, handle is (whatever the handle is).

Code: Select all

// Custom Trigger - runs given subroutine periodically and triggers when specified variable = TRUE
// In this case "DoIT" must be True to trigger "DoSomething"
OnEvent>CUSTOM,MyTriggerSub,DoIT,DoSomething
///////////////////////////////////////////////////////////
Let>k=0
Repeat>k
  Wait>0.5
  Msg>%k%
  Let>k=k+1
Until>k=200
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
SRT>MyTriggerSub
  Let>DoIT=False
  Let>nHandle=0
   // Try to get handle of a window starting with "new 4"
   GetWindowHandle>new 4*,nHandle
   IF>nHandle<>0
     Let>DoIT=True
   ENDIF
   // check something
END>MyTriggerSub
///////////////////////////////////////////////////////////
SRT>DoSomething
  MDL>Found it, handle is %nHandle%
  // do something
END>DoSomething
Phil Pendlebury - Linktree

hagchr
Automation Wizard
Posts: 314
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: GetWindowHandle Timeout?

Post by hagchr » Tue Jul 12, 2022 10:37 am

Hi! I played around with trying to find window handles and your solution with GetWindowList> seems to work fine with easy top level windows. Tried to change WF_TYPE to see if it is possible to to get all (visible, child ...)
windows but no change in result.

Then I tried to get the parent of the currently active window and get all children of that. That seems to match all items in "View System Windows" (all items with a title).

If you open the script and run it in debug mode it will pick up some garbage lines from the script. I can be avoided simply by running it from the "main control center" (ie save and run without opening it).

I am curious how these things work but swimming in deep deep water on this one. See if it is anything you can use otherwise disregard.

Code: Select all

Let>WIN_USEHANDLE=1
GetActiveWindow>strTitle,nXPos,nYPos,,
GetWindowParent>strTitle,1,hWnd
GetWindowChildList>hWnd,child_list

Let>res=

//all handles -> wins
Separate>child_list,CRLF,wins

// loop through all handles and get name and class
If>wins_count>0
Let>k=0
Repeat>k
  Let>k=k+1
  Let>this_window=wins_%k%
  if>this_window<>test
    GetWindowNames>this_window,strTitle,strClass
    Let>res=%res%%this_window%,%strTitle%,%strClass%%CRLF%
  EndIf
Until>k=wins_count

Let>MSG_WIDTH=750
MDL>res

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: GetWindowHandle Timeout?

Post by Phil Pendlebury » Tue Jul 12, 2022 11:23 am

Hi hagchr,

Thanks, it is super interesting but still confusing eh.

Yes now that window I am trying to find does indeed appear in that list. (res).

I was trying to think of away to get names from classes and your script seems to have solved it.

It is odd that getwindowhandle finds the window but getwindowlist does not.

The window I am after is I guess you can call it the parent window of the application, you don't actual see any window on screen with the name of the application, the window on screen is the name of the project (which is easy to find). And all the other child windows.

So I guess getting parent name of the project works. But the project name of course changes. :-)

I will keep experimenting and use your code as a base.

The big issue here of course is the way the application's window handling is built and it is not an application that anyone else here will have. So you cannot really test it properly and I cannot really explain it properly.

Additional:

I did originally try this:

Code: Select all

GetWindowHandle>Nameofproject,nHandle
GetWindowParent>nHandle,3,hWnd
MDL>hWnd
But the handles are not the same as what is found with, even changing 3 to 1 or 2.
Phil Pendlebury - Linktree

hagchr
Automation Wizard
Posts: 314
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: GetWindowHandle Timeout?

Post by hagchr » Tue Jul 12, 2022 1:01 pm

I added the process ID and process name (...exe etc) to the output. One more dimension but maybe still an issue if your project name keeps changing.

Code: Select all

Let>WIN_USEHANDLE=1
GetActiveWindow>strTitle,nXPos,nYPos,,
GetWindowParent>strTitle,1,hWnd
GetWindowChildList>hWnd,child_list

Let>res=

//all handles -> wins
Separate>child_list,CRLF,wins

// loop through all handles and get name and class
If>wins_count>0
Let>k=0
Repeat>k
  Let>k=k+1
  Let>this_window=wins_%k%
  if>this_window<>test
    GetWindowNames>this_window,strTitle,strClass
    GetWindowProcess>this_window,pid,pname
    Let>res=%res%%this_window%,%strTitle%,%strClass%,%pid%,%pname%%CRLF%
  EndIf
Until>k=wins_count

Let>MSG_WIDTH=750
MDL>res

User avatar
Phil Pendlebury
Automation Wizard
Posts: 518
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Re: GetWindowHandle Timeout?

Post by Phil Pendlebury » Tue Jul 12, 2022 1:16 pm

Very neat. I will do some more tests and post back again. Thank you. I am sure this will be helpful to others too. :D
Phil Pendlebury - Linktree

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts