Threads and Passing Variables in AHK_H?

Ask for help, how to use AHK_H, etc.
mattjgale
Posts: 3
Joined: 27 Oct 2017, 15:49

Threads and Passing Variables in AHK_H?

Post by mattjgale » 01 Mar 2023, 10:44

Hey all, I know this is probably fairly simple but even after a few hours of research and testing I'm still struggling to wrap my head around threads and passing variables in AHK_H so I'm hoping to get some help?

For learning - I'm simply trying to pass a variable between two scripts. Ideally I'd like this to be using threads but honestly at this point I can't even figure out how to simply store a variable in the autohotkey.dll that I can reference from other scripts so I don't want to overcomplicate my thought process with threads. So far it seems like the two ways to pass the variable is either criticalobject or ahkassign / ahkgetvar?

For testing purposes I'm trying to create two scripts - the first to simply set the variable that I can access globally, and then a second script that shows a msgbox with the new global variable.

Code: Select all

; #persistent

; Load the autohotkey.dll library
ahkDll := DllCall("LoadLibrary", "str", "AutoHotkey.dll")

; This script defines a variable and stores it in a critical object

criticalObject := CriticalObject()
myVariable := "Hello, world!"
criticalObject.SetObject(&myVariable)

msgbox %myVariable%
ExitApp ; End of script

Code: Select all

; #persistent

; Load the autohotkey.dll library
ahkDll := DllCall("LoadLibrary", "str", "AutoHotkey.dll")

; This script displays a MsgBox with the value of the critical object set in the first script

criticalObject := CriticalObject()
criticalObject.GetObject(&myVariable)
MsgBox %myVariable%

ExitApp ; End of script
Obviously I'm doing something wrong here as the msgbox displayed in the second script is blank.

The bigger use case is to multithread some imagesearches on a loop so that I can call the information returned on-demand as opposed to having to wait for each imagesearch seperately.

To do that I was thinking of something like the following:

Code: Select all

AhkThread(wabox3Thread)

; This function will run the image search loop in a separate thread
wabox3Thread()
{
    Loop
    {
	    Images := {}
        Loop, Read, HekiliSpells.csv
        Images[A_Index]:=StrSplit(A_LoopReadLine,",")
        c:=StrSplit(v,",")
  
		For k, img in Images
		{
			;wabox3 1820, 1255, 1885, 1300
			ImageSearch, FoundXWA3, FoundYWA3, 1820, 1255, 1885, 1300, % img[1]
			if ErrorLevel = 0
			{
				wabox3found := 1
				wabox3key := % img[2]
				return
			}
		}
			Sleep, 1500 ; Wait for 1.5 seconds before running the loop again
	}
}
[Mod edit: c-tags replaced with [code][/code] tags.]
This doesn't work either. I've been wracking my brain over this for a while. Can anyone provide help or at least point me in the right direction? Any idea's are genuinely appreciated!

Return to “Ask for Help”