Page 1 of 1

About Dynamic variable in AhkThread

Posted: 06 Jul 2019, 14:38
by manehscripts
Hello guys!

I'm experiencing a problem to add Dynamic variable within the AhkThread.
In the code below the program reads a .txt file that has columns and rows, I need to present the same information it presents in the CheckScript: label but it does not recognize the dynamic variable.

Script.ahk

Code: Select all

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

CheckScript:
    d=`;
    TotalLine=
    Loop, Read, %A_scriptDir%\test.txt
    {
        TotalLine = %A_Index%
        COLUMN2=
        COLUMN3=
        COLUMN4=
        COLUMN5=
        COLUMN6=
        StringSplit, COLUMN, A_LoopReadLine, %d%,
        VarCOLUMN2Line%TotalLine% = %COLUMN2%
        VarCOLUMN3Line%TotalLine% = %COLUMN3%
        VarCOLUMN4Line%TotalLine% = %COLUMN4%
        VarCOLUMN5Line%TotalLine% = %COLUMN5%
        VarCOLUMN6Line%TotalLine% = %COLUMN6%
        MsgBox, % VarCOLUMN3Line%TotalLine%  ; <------------ HERE OK
    }
return

toggle := 0
Start:
    toggle := !toggle
    if (toggle) {
        gosub, CheckScript
        VarCrit := CriticalObject({"TotalLine":TotalLine})
        script:="             
        ("          
            #NoEnv
            #NoTrayIcon
            ListLines Off
            #KeyHistory 0
            SendMode Input
            SetTitleMatchMode 2
            SetTitleMatchMode Fast
            SetDefaultMouseSpeed, 0
            Loop {
                VarCrit := CriticalObject(A_Args[1])
                CoordMode, Tooltip
                CoordMode, Pixel, Screen
                CoordMode, Mouse
                TotalLine = `% VarCrit.TotalLine
                Loop, %TotalLine%
                {
                    Count = %A_Index%
                    Col2Check = VarCOLUMN2Linha%Count%   ; <------------ DONT WORK
                    Col3Check = % VarCOLUMN3Linha%Count%   ; <------------ DONT WORK WITH %
                    Col4Check = VarCOLUMN4Linha%Count%   ; <------------ DONT WORK
                    Col5Check = % VarCOLUMN5Linha%Count%   ; <------------ DONT WORK WITH %
                    Col6Check = VarCOLUMN6Linha%Count%   ; <------------ DONT WORK
                    MsgBox, Col2Check: %Col2Check%
                    MsgBox, Col3Check: %Col3Check%
                    MsgBox, Col4Check: %Col4Check%
                    MsgBox, Col5Check: %Col5Check%
                    MsgBox, Col6Check: %Col6Check%
                }
            }
        )"
        DllOn:=AhkThread(script,&VarCrit)
    } else {
        DllOn.ahkTerminate.1
    }
return

GuiClose:
ExitApp
test.txt

Code: Select all

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

Re: About Dynamic variable in AhkThread  Topic is solved

Posted: 06 Jul 2019, 16:26
by HotKeyIt
You need to use proper Object to do that:

Code: Select all

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

CheckScript:
    d=`;
    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)
            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

Re: About Dynamic variable in AhkThread

Posted: 06 Jul 2019, 16:49
by manehscripts
Ty @HotKeyIt

I just have one more question, please.
I use the structure of my program very much like the way I presented it, and the one you showed me has some changes that I found interesting but I can not understand how to adapt, for example, new variables within the CriticalObject. I'm trying here but I can not, could you help me? How to create a variable for example Test := "Hello" and call this variable inside the Thread as a MsgBox for example, keeping this structure you made it for me.

The way I do it is like this: CriticalObject({"Test":Test, "Test2":Test2, "Test3":Test3})

Re: About Dynamic variable in AhkThread

Posted: 06 Jul 2019, 19:19
by HotKeyIt
You include them all in the object and you can add another object if needed.
You can also add more keys to the object afterwards, just make sure that you access them via CriticalObject (VarCrit).

Re: About Dynamic variable in AhkThread

Posted: 06 Jul 2019, 19:23
by manehscripts
HotKeyIt wrote:
06 Jul 2019, 19:19
You include them all in the object and you can add another object if needed.
You can also add more keys to the object afterwards, just make sure that you access them via CriticalObject (VarCrit).
Hi! Could you show an example of where to include?
You have created an Obj (Obj: = {}) I believe it is not there, below there is a loop that checks the external file.
I try too VarCrit := CriticalObject({"test":test}) | VarCrit := CriticalObject(Obj,{"test":test}) | VarCrit := CriticalObject({"Obj":Obj,"test":test}) and dont work
Idk where to include it, please. :thumbup:

Edit: I use AhkExported() and work:

Code: Select all

ParentThread := AhkExported()
MsgBox % ParentThread.AhkGetVar.test
Ty

Re: About Dynamic variable in AhkThread

Posted: 06 Jul 2019, 19:51
by HotKeyIt
Here is another example:

Code: Select all

VarCrit:=CriticalObject({test:"this is a test",test1:"this is another test"})
ahk:=AhkThread("
(
VarCrit:=CriticalObject(" (&VarCrit) ")
Loop 2 {
  out:=""""
  for k, v in VarCrit
    out.=v ""``n""
  MsgBox `% out
}
)")
Sleep 400
VarCrit.test2:="this is new test"
While ahk.ahkReady()
  Sleep 100