How to share variables between threads? Topic is solved

Ask for help, how to use AHK_H, etc.
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

How to share variables between threads?

13 Jan 2019, 18:46

Hello, I'm asking this because I couldn't make it by myself (already read the docs) and couldn't find any answer on the forum.
I'm trying to do this in Ahk_h v1*
Last edited by kyuuuri on 13 Jan 2019, 19:28, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to share variables between threads?  Topic is solved

13 Jan 2019, 18:59

since u didnt specify here it is for v2, q / w to increment from main and thread, respectively:

Code: Select all

CritObj := CriticalObject({"counter": 0})
SetTimer(() => ToolTip(CritObj.counter), 10)

Thread := AhkThread("
(
	CritObj := CriticalObject(A_Args[1])

	w::++CritObj.counter
)", &CritObj "")

q::++CritObj.counter
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

13 Jan 2019, 19:21

swagfag wrote:
13 Jan 2019, 18:59
since u didnt specify here it is for v2, q / w to increment from main and thread, respectively:

Code: Select all

CritObj := CriticalObject({"counter": 0})
SetTimer(() => ToolTip(CritObj.counter), 10)

Thread := AhkThread("
(
	CritObj := CriticalObject(A_Args[1])

	w::++CritObj.counter
)", &CritObj "")

q::++CritObj.counter
Sorry!!!!!, when I wrote "I'm trying to do this in Ahk_h" I was trying to write "I'm trying to do this in Ahk_h v1", I don't know what happened hahaha
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to share variables between threads?

13 Jan 2019, 19:30

well im on mobile rn, so cant rewrite it. swap out the timer and the continuation section. the rest is the same. or look at criticalobj in the docs
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

13 Jan 2019, 19:33

Sorry for double post.
Adapted it to AHK_H v1

Code: Select all

CritObj := CriticalObject({"counter": 0})

Thread := AhkThread("
(
	CritObj := CriticalObject(A_Args[1])

	w::
	++CritObj.counter
	tooltip, % CritObj.counter
	return
)", &CritObj "")

q::
++CritObj.counter
tooltip, % CritObj.counter
return
I can't believe it was that simple, I tried using "GlobalVarsScript" function i found before and i couldn't do anything. Is there a way to do this for a simple variable instead of an object?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to share variables between threads?

13 Jan 2019, 19:45

u can pass the address of a regular var. u can even pass the address of a regular object, it's just that u're probably gonna wanna use a CriticalSection whenever u modify it, or else u might crash, whereas CriticalObject already does that automatically.

but the section has it's uses nonetheless, maybe u wanna do some transaction
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

13 Jan 2019, 19:58

Nice, thank you. 1 last question in case you can answer (if not I will open another topic).

When I declare my code as a variable like this:

Code: Select all

asdasd =
(
; 562 lines code here
)
I get "variable name too long". Does this mean that the content (not the name) of the variable is too long? or is this a bug?
Is there a way to have that 562 lines code inside this script to be launched on another thread?
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

13 Jan 2019, 21:25

Thank you, 1 last question that I couldn't find on the docs:

What's the correct way to translate a normal script to an script inside a continuation section, for example:

Code: Select all

test =
(
process1 = asd
msgbox Process %process1% not found.
)
Shows nothing, like if %process1% was blank but

Code: Select all

test =
(
process1 = asd
msgbox Process `%process1`% not found.
)
This works.
But also, replacing every "%" with "`%" is not the answer because it breaks the script.
So what rule should I follow when replacing "%" with "`%".
Thank you
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to share variables between threads?

14 Jan 2019, 00:42

look up the escaping rules in the documentation. depending on what exactly ure doing with the scripts, u might need to account for several nested escape levels

ofc u can always just call the script from file instead
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

15 Jan 2019, 02:05

Hello, I'm trying to do the following:
CritObj := CriticalObject({"answer": 0, "on": 1})
And when i do CritObj.on = 1 on a thread it shows an error: "CritObj.on Does not contaign a recognized action", but if I do CritObj.answer = 1 it works.
To create the thread I'm doing the following: asd := AhkThread(test, &CritObj)
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

15 Jan 2019, 05:33

swagfag wrote:
15 Jan 2019, 05:15
show all scripts

Code: Select all

CritObj := CriticalObject({"answer": 0, "on": 1})
test = 
(
	CritObj := CriticalObject(A_Args[1])
	CritObj.answer = 1
	CritObj.on = 1
	msgbox `% CritObj.answer " and " CritObj.on
)
return

f1::
tooltip % CritObj.answer " and " CritObj.on
asd := AhkThread(test, &CritObj)
return
There you go. I already read CriticalObject and AhKThread docs as well but I can't find anything about this.

Thanks in advance.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to share variables between threads?

15 Jan 2019, 06:03

=, oh ure assigning, i assumed u were comparing..
u cant use the legacy assignment to assign to object members, use := instead
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: How to share variables between threads?

15 Jan 2019, 06:21

I feel stupid, but that was an small detail hahaha. Thank you!
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to share variables between threads?

06 Jul 2019, 18:28

swagfag wrote:
13 Jan 2019, 18:59
since u didnt specify here it is for v2, q / w to increment from main and thread, respectively:

Code: Select all

CritObj := CriticalObject({"counter": 0})
SetTimer(() => ToolTip(CritObj.counter), 10)

Thread := AhkThread("
(
	CritObj := CriticalObject(A_Args[1])

	w::++CritObj.counter
)", &CritObj "")

q::++CritObj.counter
Hi @swagfag

I have a question, please.
It's been a while since I found this example and I'm using it until today with some changes only.
Today I requested a help and HotKeyIt gave me a new code, which I'm thinking of using. Based on your example, could you tell me how I add other variables within the thread? In the example below it get from a list, but I wanted to get other external variables, taking advantage of this code, can you help me?

Code: Select all

Gui, Font, s30
Gui, Add, Button, w200 h80 gStart, Start
Gui, Show, , Dynamic variable in AhkThread
return

CheckScript:
    VARIABLETEST := "ABC"   ;<-------------------------- GET THIS VARIABLE
    Obj:={}
    file:="
(
1;1;+1;NoCondition;Wait;8
2;5;-2;NoCondition;Wait;8
3;1;0;NoCondition;Wait;8
4;2;-5;NoCondition;Wait;8
5;3;+7;NoCondition;Wait;8
)"
    Loop, Parse, file, `n`r ;Loop, Read, %A_scriptDir%\test.txt
        Obj.Push(StrSplit(A_LoopField,";"))
return

toggle := 0
Start:
    toggle := !toggle
    if (toggle) {
        gosub, CheckScript
        VarCrit := CriticalObject(Obj)
        script:="              
        ("          
            #NoEnv
            #NoTrayIcon
            ListLines Off
            #KeyHistory 0
            SendMode Input
            SetTitleMatchMode 2
            SetTitleMatchMode Fast
            SetDefaultMouseSpeed, 0
            Obj := CriticalObject(A_Args[1]+0)
            MsgBox, %VARIABLETEST%  ;<-------------------------- MSGBOX HERE
            Loop {
                CoordMode, Tooltip
                CoordMode, Pixel, Screen
                CoordMode, Mouse
                for k,v in Obj
                {
                    MsgBox `% ""Col1: "" v.1 ""``nCol1: "" v.2 ""``nCol1: "" v.3 ""``nCol1: "" v.4 ""``nCol1: "" v.5 ""``nCol1: "" v.6
                }
            }
        )"
        DllOn:=AhkThread(script,(&VarCrit) "")
    } else {
        DllOn.ahkTerminate()
    }
return

GuiClose:
ExitApp
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to share variables between threads?

06 Jul 2019, 19:39

Code: Select all

; v1
VARIABLETEST := "ABC"

script =
(LTrim %
	ParentThread := AhkExported()
	MsgBox % ParentThread.AhkGetVar.VARIABLETEST
)
DllOn := AhkThread(script)

MsgBox Exit?
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to share variables between threads?

06 Jul 2019, 19:44

swagfag wrote:
06 Jul 2019, 19:39

Code: Select all

; v1
VARIABLETEST := "ABC"

script =
(LTrim %
	ParentThread := AhkExported()
	MsgBox % ParentThread.AhkGetVar.VARIABLETEST
)
DllOn := AhkThread(script)

MsgBox Exit?
Hey bro, thanks, perfect! :dance: :wave: :thumbup:
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 13 guests