05-26-2014, 05:01 PM
In this example other thread waits for Notepad window and then temporarily suspends main macro.
Macro Macro2325
Function thread_wait_window
SuspendThread can be dangerous, the two threads can lock each other. It depends on what code is executed in main thread and what code is executed in other thread while main thread is suspended. But this is the easiest solution. Else main thread would have to periodically check if it should wait.
Macro Macro2325
;start thread that waits for a window or something and then temporarily suspends this thread
QMTHREAD qt; GetQmThreadInfo(0 &qt)
mac "thread_wait_window" "" qt.threadhandle
;main macro code example
int i
for i 0 1000
,1
,out iFunction thread_wait_window
;\
function mainThreadHandle
atend ResumeThread mainThreadHandle
rep
,;wait for some condition, eg window created and visible
,rep
,,0.01
,,if(WaitForSingleObject(mainThreadHandle 0)!=WAIT_TIMEOUT) ret ;;macro ended
,,int w=win("Notepad" "Notepad" "" 0x400) ;;check the condition (edit this line)
,,if(w) break
,
,;suspend main thread
,SuspendThread(mainThreadHandle)
,
,;do something with the window (replace this code with your code)
,sel mes("Now macro is suspended.[][]OK - close window and resume macro.[]Cancel - end macro." "" "OC")
,,case 'O'
,,clo w; err
,,0.1
,,
,,case else
,,EndThread "" mainThreadHandle
,,ret
,
,;resume main thread
,ResumeThread(mainThreadHandle)SuspendThread can be dangerous, the two threads can lock each other. It depends on what code is executed in main thread and what code is executed in other thread while main thread is suspended. But this is the easiest solution. Else main thread would have to periodically check if it should wait.
