Running Two Commands Simultaneously, I can only get the button I press first to work Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dxllon
Posts: 2
Joined: 08 Feb 2018, 18:42

Running Two Commands Simultaneously, I can only get the button I press first to work

08 Feb 2018, 18:50

Code: Select all

-----------------Gui--------------

Gui, Show , w150 h150, Window title

; here you have a text, button and edit
Gui, Add, Text, x10 y10 w90 Center,Bot
Gui, Add, Button, x10 y52 w90 h20 vFIRSTBUTTON ,Walk
Gui, Add, Button, x10 y90 w90 h20 vSECONDBUTTON ,Click
Return

ButtonWalk:
Loop,
{Send {Right Down} 
sleep, 4000
Send {Right Up} ; IMO this should be included. YMMV
Send {Left Down} 
sleep, 2000
Send {Left Up} ; IMO this should be included. YMMV
}
return

ButtonClick:
Loop
{
click
sleep 2000
click
sleep 2000
}
return

GuiClose: 
ExitApp
[Moderator's note: Duplicate topic merged. Code tags fixed.]
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Running Two Commands Simultaneously, I can only get the button I press first to work

08 Feb 2018, 19:15

Due to how threads operate, what you'll be more interested in is a SetTimer to turn on ButtonWalk and ButtonClick to execute every 6000/4000 milliseconds.

With the Loops as you have them now, AHK will execute Thread A, then interrupt it to run Thread B. It will not do anything to Thread A until Thread B is complete -- which is signaled by a return. Because you're in an infinite loop, return is never reached, which means Thread B never stops, which means Thread A is never resumed.

But SetTimer does take advantage of the return in the target label, so the two threads would take turns. SetTimer can keep starting the thread over again automatically for you.
Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Running Two Commands Simultaneously, I can only get the button I press first to work  Topic is solved

09 Feb 2018, 01:47

Hallo,
a second thread blocks the first while it is still running.
See https://autohotkey.com/docs/misc/Threads.htm and use timers instead of loops.
Try:

Code: Select all

Gui, Show , w150 h150, Window title

; here you have a text, button and edit
Gui, Add, Text, x10 y10 w90 Center,Bot
Gui, Add, Button, x10 y52 w90 h20 vFIRSTBUTTON ,Walk
Gui, Add, Button, x10 y90 w90 h20 vSECONDBUTTON ,Click
Return

ButtonWalk:
	SetTimer, Walk, 7000
Return
Walk:
	Send {Right Down}
	sleep, 4000
	Send {Right Up} ; IMO this should be included. YMMV
	Send {Left Down}
	sleep, 2000
	Send {Left Up} ; IMO this should be included. YMMV
Return

ButtonClick:
	SetTimer, Click, 2000
Return
Click:
	click
Return

GuiClose:
ExitApp
Dxllon
Posts: 2
Joined: 08 Feb 2018, 18:42

Re: Running Two Commands Simultaneously, I can only get the button I press first to work

09 Feb 2018, 07:40

Thank you so much man it works perfectly! @Rohwedder

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 170 guests