Wait

Syntax

[wait ]ns

 

Parts

ns - number of seconds to wait. Type - double. Must be greater than zero, or:

0 or omitted process messages that maybe are waiting in thread's message queue (as DoEvents in Visual Basic), regardless of opt waitmsg.
-1 infinite.
-2 autodelay. Apply caller's speed or, if there is no direct caller, default speed (as set in Options).

 

Remarks

Wait ns seconds.

 

If ns is simple digits, or begins with a digit, the wait keyword can be omitted.

 

QM 2.2.1: The wait time precision is about 2 milliseconds (0.002 s). For example, wait 0.001 will probably wait 2 ms. In previous versions, normally it was 5 - 15 ms, although sometimes better (depends on OS and some running programs).

 

Note: Don't use Windows API wait functions, because then QM cannot properly end the thread on user request. It then terminates thread and displays warning in the output.

 

Examples

 Wait 0.5 seconds:
wait 0.5
 or:
0.5

 Wait 15 seconds; the wait time is variable wt:
int wt=15
wait wt

 Wait 2 seconds:
double F=0.5
4*F

 


Wait for event

Syntax

[wait ]ns event [...]

 

Can be used as function:

 

int wait(ns event [...])

 

Parts

ns - max number of seconds to wait. Integer (e.g. 5) or double (e.g. 0.5).

event - one of literals from the table below (WA, etc).

... - more parts. Depends on event. To understand easier, see examples at first.

 

event ... Comments
WA window to wait for. Wait until window is active, visible and not minimized. If window does not exist, wait until it is created. WA is optional. Returns window handle. See also: run.
-WA or WN the same Wait until window is deactivated or destroyed. Don't wait if window is already inactive or does not exist. Returns handle of active window.
WC the same Wait until window is created. Don't wait if window already exists. Returns window handle.
-WC or WD the same Wait until window is destroyed. Don't wait if window does not exist. Returns handle of active window.
WP the same Wait until window's program exits. Don't wait if window does not exist. Returns handle of active window. See also: run.
WV the same Wait until window is visible. If window does not exists, at first wait until it is created. Returns window handle.
-WV the same Wait until window is hidden or destroyed. Returns window handle. If window does not exist, does not wait and returns 1.
WE the same Wait until window is enabled. If window does not exists, at first wait until it is created. Returns window handle.
-WE the same Wait until window is disabled or destroyed. Returns window handle. If window does not exist, does not wait and returns 1.
WAI list of windows, e.g., "Notepad[]WordPad" Wait until one of windows in the list is active, visible and not minimized. Returns 1-based window index in the list.
     
K [keycode] - single QM key code. Optional.

Wait for key up event. If keycode is omitted, wait for any key. keycode also can be string variable in parentheses (QM key code), or numeric value in parentheses (virtual-key code). Returns virtual-key code.

KF the same Wait for key down event, and filter (eat) it. Returns virtual-key code. Not available in exe.
     
ML   Wait for left mouse button up event. Returns 1.
MR   Wait for right mouse button up event. Returns 2.
MM   Wait for middle mouse button up event. Returns 4.
M   Wait for any mouse button up event. Returns mouse button code: 1 left, 2 right, 4 middle, 5 X1, 6 X2.
     
P [threshold] - CPU usage level, 1 to 100 %. Wait until CPU usage is less than threshold. If threshold is 0 or omitted, is used value specified in Options (default is 20 %). See also: GetCPU.
-P [threshold] - CPU usage level, 0 to 99 %. Wait until CPU usage is more than threshold.
     
H handle - synchronization object handle. Integer. Wait until the synchronization object becomes signaled. For example, can wait until another thread exits (see examples). Returns 1, or -1 if the handle is invalid (before QM 2.2.0 was 0).
HM

ha - variable of type ARRAY(int). Or:

h1 h2 [h3] [h4] - 2 to 4 handles.

QM 2.2.0. Wait until one of the synchronization objects becomes signaled. ha can contain maximum 60 elements. Returns the index of the first signaled handle + 1. Returns -1 if a handle is invalid.
     
V variable - variable of type int+. Wait until the variable becomes nonzero. Returns the final value of the variable.
-V the same Wait until the variable becomes 0. Returns 0.
     
I [url] - wait for this URL. If omitted, waits while browser is busy. Wait until web page is finished loading and browser isn't busy. To have more control, use web. Note: waits only on single window (if matching URL is opened in new window, ignores it).
     
CU cursor - cursor id. Wait for the specified cursor (mouse pointer). Cursor id constants (IDC_...) are defined in WinConstants.
-CU the same Wait for a different cursor.
     
C color x y [window] [flags]

Wait for the specified color in the specified point in window or screen (if window is 0 or omitted). See pixel.

-C the same Wait for a different color.
     
S file [window] [rect] [flags] Wait for an image on the screen. See scan. QM 2.2.1: file also can be bitmap/icon handle.
-S the same

Wait until image disappears.

 

QM 2.2.1: If file is "", waits until something changes in the window or rectangle.

 

Remarks

Waits for the specified event maximum ns seconds (ns<=0 means infinite). If the event does not occur within ns, macro ends (run-time error). To continue macro, use err statement.

 

If ns is simple digits, or begins with a digit, the wait keyword can be omitted.

 

The wait time precision depends on event. For key, mouse and H events, it is 0 - 10 ms. For others - from 20 to 300 ms.

 

Can be used as function, e.g. variable=wait(...). The return value depends on event. If not specified in the table, returns 1. If opt waitmsg was set, returns 0 if the thread receives WM_QUIT message.

 

On Vista, waiting for key and mouse does not work if the active window has higher integrity level (eg QM - standard user, window - administrator).

 

Tips

Functions mes, list, inp, modal dialogs and menus also are "wait for" functions, because they wait until are closed.

 

Waiting for other events can be easily implemented with rep (see example). To create new "wait for" user-defined functions, you can use WaitFor template.

 

Macro that contains "wait for CPU" commands, or opt waitcpu 1, sometimes may run very slowly or produce "wait timeout" errors. This is because some programs constantly or occasionally use 100% CPU, even when they are ready for input.

 

Waiting for image (S, -S) in large region (whole screen or window) consumes significant amount of CPU time. If during that time other application performs some processing, QM may slow down it. If processing speed is important, you can view CPU usage of qm.exe, and, if it is too high, minimize it by minimizing the search region (read more). While waiting, QM periodically searches for the image. If speed (spe) is 0, the rate is much higher.

 

To minimize CPU usage when waiting for a window, use window class. For example, to wait for Internet Explorer, instead of wait 0 "Internet Explorer" use wait 0 "+IEFrame" or wait 0 win("Internet Explorer" "IEFrame"). Finding window by class is much faster.

 

To see CPU usage, use Windows Task Manager or perfmon.exe. You can also use GetCPU function.

 

Examples

 Wait for window "Notepad" max. 5 seconds; on timeout error:
wait WA 5 "Notepad"
 or (WA can be omitted):
wait 5 "Notepad"
 or (wait can be omitted):
5 WA "Notepad"
 or (WA and wait can be omitted):
5 "Notepad"

 Wait for window "Notepad" max. 5 seconds; on timeout continue:
wait WA 5 "Notepad"; err

 Wait max 5 s for window "Notepad"; on timeout run macro "Open":
wait 5 WA "Notepad"
err
	mac- "Open"

 Wait until window "Notepad" is closed:
wait 0 -WC "Notepad"

 Wait until window with name "Calc" and class "SciCalc" is created:
wait 0 WC win("Calc" "SciCalc")

 Wait until control with id 1500 in window "Window" becomes enabled:
wait 0 WE id(1500 "Window")

 Wait for window "Notepad" max. 5 seconds; store window handle into variable:
int hwnd = wait(5 WA "Notepad"); err

 Wait until currently active window is deactivated (another window  becomes active); return handle of another window:
int hwnd = wait(5 -WA "")

 Wait for window "Error" or "Warning" and detect which of them is activated:
opt err 1
sel wait(5 WAI "Error[]Warning")
	case 1 out "Error"
	case 2 out "Warning"
	case 0 out "timeout"

 Wait for message "Error" or "Warning" that belongs to "APP" program:
int hwnd = wait(5 win("Error[]Warning" "#32770" "APP" 16))

 Wait for key F12:
wait 0 K F12

 Wait for any key, filter it, and display virtual-key code:
int vk = wait(0 KF)
out vk

 Run program and max. 15 seconds wait until CPU usage is less than 20%:
run "app.exe"; wait 15 P 20

 Wait for some condition that is not supported by wait:
rep() if(condition) break; else 0.1

 Wait for mouse left button down:
rep() 0.02; ifk((1)) break

 Run notepad and wait until it exits (just example of wait 0 H; use run with flag 0x400 instead):
int hProcess=run("notepad.exe")
wait 0 H hProcess; CloseHandle hProcess

 Run function "Function" in separate thread and wait until it exits:
int hThread=mac("Function")
wait 0 H hThread ;;do not close the handle!

 Create event and wait until other thread sets it:
 ______Thread1_______
int+ g_hevent
if(!g_hevent) g_hevent=CreateEvent(0 0 0 0)
wait 60 h g_hevent
bee
 ______Thread2_______
int+ g_hevent
SetEvent(g_hevent)

 Wait until one of two events is set:
int+ g_e1 g_e2
if(!g_e1) g_e1=CreateEvent(0 0 0 0)
if(!g_e2) g_e2=CreateEvent(0 0 0 0)
int i=wait(0 HM g_e1 g_e2)
out i