How to add a lib to script?

Ask for help, how to use AHK_H, etc.
DoriTos_
Posts: 16
Joined: 15 Mar 2021, 08:30

How to add a lib to script?

Post by DoriTos_ » 17 Oct 2021, 20:56

Hello, i'm trying to put a lib file inside the script, without #include, all in the main script.

If I run 2 threads+ using same lib and Alias my script crash.

Can someone help me, is there a way to do this?

Example script:

Code: Select all

#Persistent
#SingleInstance, Force
SetBatchLines, -1

test:=CriticalObject()

test.lib_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1

    ; 1000 lines lib
    ; exe:=AhkExported()
    ; addScript(exe.ahkFunction(""get_lib()""), true)

    ;lib e.g
    class lib_test
    {

        func_test(a) { 
            return %a%
        }
        
    }
)")

test.thread_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    exe:=AhkExported()
    test:=CriticalObject(" (&test) ")

    Alias( go_1, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )

    Loop, {
        Tooltip, % go_1.func_test(""thread_1 | loop: "" . a_index), 0, 50
        Sleep, 10
    }
)")

test.thread_2:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    test:=CriticalObject(" (&test) ")

    Alias( go_2, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )

    Loop, {
        Tooltip, % go_2.func_test(""thread_2 | loop: "" . a_index), 0, 100
        Sleep, 10
    }
)")

test.thread_3:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    test:=CriticalObject(" (&test) ")

    Alias( go_3, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )

    Loop, {
        Tooltip, % go_3.func_test(""thread_3 | loop: "" . a_index), 0, 150
        Sleep, 10
    }
)")
*ahk_H v1

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to add a lib to script?

Post by HotKeyIt » 18 Oct 2021, 09:01

Code: Select all

#Persistent
#SingleInstance, Force
SetBatchLines, -1

test:={}

test.lib_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    ready:=1
    class lib_test
    {
        func_test(a) { 
            return `%a`%
        }
    }
)")
While !test.lib_1.ahkgetvar("ready")
  Sleep 100
test.thread_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    exe:=AhkExported()
    test:=CriticalObject(" (&test) ")
    Alias( go_1, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )
    Loop, {
        Tooltip, % ""info: "" go_1.func_test(""go_1""), 0, 50
        Sleep, 10
    }
)")

test.thread_2:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    test:=CriticalObject(" (&test) ")
    Alias( go_2, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )
    Loop, {
        Tooltip, % ""info: "" go_2.func_test(""go_2""), 0, 100
        Sleep, 10
    }
)")

test.thread_3:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    test:=CriticalObject(" (&test) ")
    Alias( go_3, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )
    Loop, {
        Tooltip, % ""info: "" go_3.func_test(""go_3""), 0, 150
        Sleep, 10
    }
)")
MsgBox
EDIT:
That is how I would do it:

Code: Select all

#Persistent
#SingleInstance, Force
SetBatchLines, -1

test:={}

test.lib_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    ptr:=&lib_test
    class lib_test
    {
        func_test(a) { 
            return `%a`%
        }
    }
)")
While !test.lib_1.ahkgetvar("ptr")
  Sleep 100
CriObjPtr:=test.lib_1.ahkgetvar("ptr")+0
test.thread_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    exe:=AhkExported()
    go_1:=CriticalObject(" CriObjPtr ")
    Loop, {
        Tooltip, % ""info: "" go_1.func_test(""go_1""), 0, 50
        Sleep, 10
    }

)")

test.thread_2:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    go_2:=CriticalObject(" CriObjPtr ")
    Loop, {
        Tooltip, % ""info: "" go_2.func_test(""go_2""), 0, 100
        Sleep, 10
    }

)")

test.thread_3:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    go_3:=CriticalObject(" CriObjPtr ")
    Loop, {
        Tooltip, % ""info: "" go_3.func_test(""go_3""), 0, 150
        Sleep, 10
    }

)")
MsgBox

DoriTos_
Posts: 16
Joined: 15 Mar 2021, 08:30

Re: How to add a lib to script?

Post by DoriTos_ » 18 Oct 2021, 15:39

HotKeyIt wrote:
18 Oct 2021, 09:01

Code: Select all

#Persistent
#SingleInstance, Force
SetBatchLines, -1

test:={}

test.lib_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    ready:=1
    class lib_test
    {
        func_test(a) { 
            return `%a`%
        }
    }
)")
While !test.lib_1.ahkgetvar("ready")
  Sleep 100
test.thread_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    exe:=AhkExported()
    test:=CriticalObject(" (&test) ")
    Alias( go_1, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )
    Loop, {
        Tooltip, % ""info: "" go_1.func_test(""go_1""), 0, 50
        Sleep, 10
    }
)")

test.thread_2:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    test:=CriticalObject(" (&test) ")
    Alias( go_2, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )
    Loop, {
        Tooltip, % ""info: "" go_2.func_test(""go_2""), 0, 100
        Sleep, 10
    }
)")

test.thread_3:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    test:=CriticalObject(" (&test) ")
    Alias( go_3, test.lib_1.ahkgetvar( ""lib_test"", 1 ) )
    Loop, {
        Tooltip, % ""info: "" go_3.func_test(""go_3""), 0, 150
        Sleep, 10
    }
)")
MsgBox
EDIT:
That is how I would do it:

Code: Select all

#Persistent
#SingleInstance, Force
SetBatchLines, -1

test:={}

test.lib_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    ptr:=&lib_test
    class lib_test
    {
        func_test(a) { 
            return `%a`%
        }
    }
)")
While !test.lib_1.ahkgetvar("ptr")
  Sleep 100
CriObjPtr:=test.lib_1.ahkgetvar("ptr")+0
test.thread_1:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    exe:=AhkExported()
    go_1:=CriticalObject(" CriObjPtr ")
    Loop, {
        Tooltip, % ""info: "" go_1.func_test(""go_1""), 0, 50
        Sleep, 10
    }

)")

test.thread_2:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    go_2:=CriticalObject(" CriObjPtr ")
    Loop, {
        Tooltip, % ""info: "" go_2.func_test(""go_2""), 0, 100
        Sleep, 10
    }

)")

test.thread_3:=Ahkthread("
(
    #Persistent
    SetBatchLines, -1
    go_3:=CriticalObject(" CriObjPtr ")
    Loop, {
        Tooltip, % ""info: "" go_3.func_test(""go_3""), 0, 150
        Sleep, 10
    }

)")
MsgBox
Thanks for answering, but both scripts are crashing here, they just close with no error msg

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to add a lib to script?

Post by HotKeyIt » 18 Oct 2021, 17:11

Which version are you using?
I just downloaded latest version and it works without problems (tested on Win32w).

DoriTos_
Posts: 16
Joined: 15 Mar 2021, 08:30

Re: How to add a lib to script?

Post by DoriTos_ » 18 Oct 2021, 19:58

HotKeyIt wrote:
18 Oct 2021, 17:11
Which version are you using?
I just downloaded latest version and it works without problems (tested on Win32w).
The lastest x64w_MT

i figured something out

If i put one lib script inside each thread It works normally and won't crash, but this will make the script use a lot of ram, just imagine running 10+ findtext libs at same time lol

I'm trying to clean the thread but I couldn't... the memory usage doesn't go down if I'm using a big lib

what I'm using is

Code: Select all

ahkthread_free(lib_thread_1)
thread_1:=""
why it doesn't clean the memory ?

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to add a lib to script?

Post by HotKeyIt » 19 Oct 2021, 03:13

It seems to clear memory as far as I can see:

Code: Select all

script:="#Persistent`n"
Loop % 102400
   script.="var" A_Index ":=" A_Index "`n"
thread_1:=AhkThread(script)
MsgBox started
thread_1.ahkterminate()
ahkthread_free(lib_thread_1)
thread_1:=""
MsgBox freed

Post Reply

Return to “Ask for Help”