Threads exchanging variables

Ask for help, how to use AHK_H, etc.
Galaxis
Posts: 73
Joined: 04 Feb 2016, 20:09

Threads exchanging variables

02 May 2018, 13:09

What is a very basic way to have the 3rd thread identify that the Sky is blue, and time is 10am. Perhaps I wrote the code wrong because I'm getting empty message boxes . :problem:
I don't want to have to use ini files. :facepalm:

Copied a line in the example documentation, but no dice.
Are aliases more appropriate for what I am trying, instead of the code below? https://hotkeyit.github.io/v2/docs/commands/Alias.htm

Code: Select all

Thread1:=AhkThread("Sky := Blue")
Thread2:=AhkThread("Time := 10am") 

Thread3:=AhkThread("
(
	
	Msgbox % Thread1.ahkgetvar.Sky
	Msgbox % Thread2.ahkgetvar.Time

)")  
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Threads exchanging variables

02 May 2018, 16:42

i cant help u further than that, but here are my findings:

1. Thread 1 and 2 the way theyre written in you original script exit immediately after the assignment, so Thread 3 wouldnt have had the chance to read the values even if it could do so correctly in the first place
2. Blue and 10am need to be escaped, else all youre doing is assigning the value of the variables Blue and 10am to Sky and Time, respectively
3. i couldnt for the life of me get ahkassign or alias to work. The only way was to retrieve the variable contents with ahkgetvar and pass them as command arguments to the newly created Thread3, which is just a janky workaround

Code: Select all

Thread1:=AhkThread("#Persistent`nSky := ""Blue""")
Thread2:=AhkThread("#Persistent`nTime := ""10am""") 

Thread3:=AhkThread("
(
	#Persistent
	Msgbox % A_Args[1]
	Msgbox % A_Args[2]
)", Thread1.ahkgetvar.Sky . " " . Thread2.ahkgetvar.Time) 

Esc::ExitApp
here are my experiments:

Code: Select all

varAliasPending := 100 ; we'll be changing 'var' to this later on

Thread := AhkThread()
Thread.ahkassign("var", 50)
Thread.ahkExec("Msgbox % var") ; v1 works
; Thread.ahkExec("Msgbox(var)") ; v2 implodes, ACCESS_VIOLATION in line 4: ahkassign, why??

Alias(var, varAliasPending) ; time to change 'var' to equal 100
Thread.ahkExec("Msgbox % var") ; not working, 'var' still = 50
Alias(varAliasPending, var) ; maybe do it the other way around
Thread.ahkExec("Msgbox % var") ; not working, 'var' still = 50
Alias(var, getvar(varAliasPending)) ; ok, how about this
Thread.ahkExec("Msgbox % var") ; not working, 'var' still = 50
Alias(varAliasPending, getvar(var)) ; what even am i doing rn
Thread.ahkExec("Msgbox % var") ; not working, 'var' still = 50
maybe one of the 5 ppl on earth who understand ahk_h can shed some light
Galaxis
Posts: 73
Joined: 04 Feb 2016, 20:09

Re: Threads exchanging variables

02 May 2018, 17:23

Thanks for your efforts <3
I took the chicken way out, and just have all of the threads access and write values to a single .Ini file. Although I wanted to be fancy and have threads talk directly- the ini method is a surefire way to get threads to exchange their variable values. I'm writing Street Fighter V and Final Fantasy XV programs- and converting what was originally in AHK v2. I jumped into AHK version H, simply because the multithreading can combine all my many scripts into one. Street Fighter has over 40 characters, so I didn't want 40 different scripts, LOL :crazy:

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 20 guests