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*
How to share variables between threads? Topic is solved
How to share variables between threads?
Last edited by kyuuuri on 13 Jan 2019, 19:28, edited 1 time in total.
Re: How to share variables between threads? Topic is solved
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
Re: How to share variables between threads?
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 hahahaswagfag wrote: ↑13 Jan 2019, 18:59since 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
Re: How to share variables between threads?
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
Re: How to share variables between threads?
Sorry for double post.
Adapted it to AHK_H v1
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?
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
Re: How to share variables between threads?
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
but the section has it's uses nonetheless, maybe u wanna do some transaction
Re: How to share variables between threads?
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:
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?
When I declare my code as a variable like this:
Code: Select all
asdasd =
(
; 562 lines code here
)
Is there a way to have that 562 lines code inside this script to be launched on another thread?
Re: How to share variables between threads?
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.
)
Code: Select all
test =
(
process1 = asd
msgbox Process `%process1`% not found.
)
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
Re: How to share variables between threads?
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
ofc u can always just call the script from file instead
Re: How to share variables between threads?
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)
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)
Re: How to share variables between threads?
show all scripts
Re: How to share variables between threads?
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
Thanks in advance.
Re: How to share variables between threads?
=, oh ure assigning, i assumed u were comparing..
u cant use the legacy assignment to assign to object members, use := instead
u cant use the legacy assignment to assign to object members, use := instead
Re: How to share variables between threads?
I feel stupid, but that was an small detail hahaha. Thank you!
- manehscripts
- Posts: 126
- Joined: 03 May 2019, 16:10
Re: How to share variables between threads?
Hi @swagfagswagfag wrote: ↑13 Jan 2019, 18:59since 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
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!
Stop to think, shut up to resist, and act to win!
Re: How to share variables between threads?
Code: Select all
; v1
VARIABLETEST := "ABC"
script =
(LTrim %
ParentThread := AhkExported()
MsgBox % ParentThread.AhkGetVar.VARIABLETEST
)
DllOn := AhkThread(script)
MsgBox Exit?
- manehscripts
- Posts: 126
- Joined: 03 May 2019, 16:10
Re: How to share variables between threads?
Hey bro, thanks, perfect!swagfag wrote: ↑06 Jul 2019, 19:39Code: Select all
; v1 VARIABLETEST := "ABC" script = (LTrim % ParentThread := AhkExported() MsgBox % ParentThread.AhkGetVar.VARIABLETEST ) DllOn := AhkThread(script) MsgBox Exit?
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
Stop to think, shut up to resist, and act to win!
Who is online
Users browsing this forum: No registered users and 13 guests