Help with multi-threading AHK Script

Ask for help, how to use AHK_H, etc.
User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Help with multi-threading AHK Script

Post by ibieel » 20 Nov 2021, 09:27

Hello guys, I want to use AHK V2 to run the labels at the same time using only 1 AHK. (multiples routine in one script)
it's possible?

I'm creating a script that needs to have fast reactions, whenever a certain pixel changes on the screen it should do the action I put as fast as possible.
I'm currently using AHK V1, to do this I need to open 3 AHK Script to run 3 Labels at the same time.

Could someone give me examples of how I can do this in AHK V2 without running 3 AHK Script?

Example:

Code: Select all

SetTimer, Label1, On
SetTimer, Label2, On
Label1:
   ToolTip, Label1 Start, 250, 250, 1
   Sleep 1000
RETURN

Label2:
   ToolTip, Label2 Start, 500, 500, 2
   Sleep 1000
RETURN

for performance reasons, I would like the script to run Label1 and Label2 at the same time.

User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: Help with multi-threading AHK Script

Post by ibieel » 26 Nov 2021, 18:39

any helpp????

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Help with multi-threading AHK Script

Post by HotKeyIt » 26 Nov 2021, 21:57

To start a new thread simply use AhkThread but you will need AHK_H.
Up to date AHK_H v2 will be available very soon but you can also use the latest one for the moment: AHK_H v2 or AHK_H v1.
Example for v1 where 2 Thread start kind of 'at the same time':

Code: Select all

ahk1:=AhkThread("#Persistent`nReturn`nLabel:`nToolTip Script 1, 250, 250`nReturn")
ahk2:=AhkThread("#Persistent`nReturn`nLabel:`nToolTip Script 2, 500, 500`nReturn")
MsgBox Click Ok To run the label in threads
ahk1.ahkLabel("Label"),ahk2.ahkLabel("Label")
MsgBox end

User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: Help with multi-threading AHK Script

Post by ibieel » 28 Nov 2021, 19:35

HotKeyIt wrote:
26 Nov 2021, 21:57
To start a new thread simply use AhkThread but you will need AHK_H.
Up to date AHK_H v2 will be available very soon but you can also use the latest one for the moment: AHK_H v2 or AHK_H v1.
Example for v1 where 2 Thread start kind of 'at the same time':

Code: Select all

ahk1:=AhkThread("#Persistent`nReturn`nLabel:`nToolTip Script 1, 250, 250`nReturn")
ahk2:=AhkThread("#Persistent`nReturn`nLabel:`nToolTip Script 2, 500, 500`nReturn")
MsgBox Click Ok To run the label in threads
ahk1.ahkLabel("Label"),ahk2.ahkLabel("Label")
MsgBox end
FULL CODE IS THIS ?!

Code: Select all

SetTimer, Label1, On
SetTimer, Label2, On

ahk1:=AhkThread("#Persistent`nReturn`nLabel:`nToolTip Script 1, 250, 250`nReturn")
ahk2:=AhkThread("#Persistent`nReturn`nLabel:`nToolTip Script 2, 500, 500`nReturn")
MsgBox Click Ok To run the label in threads
ahk1.ahkLabel("Label"),ahk2.ahkLabel("Label")
MsgBox end

Label1:
   ToolTip, Label1 Start, 250, 250, 1
   Sleep 1000
RETURN

Label2:
   ToolTip, Label2 Start, 500, 500, 2
   Sleep 1000
RETURN

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Help with multi-threading AHK Script

Post by HotKeyIt » 29 Nov 2021, 17:39

No, if you want repeating label, simply use loop:

Code: Select all

ahk1:=AhkThread("#Persistent`nReturn`nLabel:`nLoop {`nToolTip Script 1 `%A_Index`%, 250, 250`nSleep 100`n}`nReturn")
ahk2:=AhkThread("#Persistent`nReturn`nLabel:`nLoop {`nToolTip Script 2 `%A_Index`%, 500, 500`nSleep 100`n}`nReturn")
MsgBox Click Ok To run the label in threads
ahk1.ahkLabel("Label"),ahk2.ahkLabel("Label")
MsgBox end

Post Reply

Return to “Ask for Help”