NOOB guide on how to use H ? Topic is solved

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
User avatar
Kintaro-OEx
Posts: 12
Joined: 17 Apr 2018, 15:48

NOOB guide on how to use H ?

Post by Kintaro-OEx » 19 Nov 2020, 15:39

Hello together!

I am really running into a wall to start using version H. Simply because it is not a normal installation like the normal version of AHK.

My main goal is to proceed using multiple THREADS and not timer which are still single threaded and infinite loops do not work then obviously.

I truly struggle to find any helpful stuff with Google.

Would love you so much for some guidance!!!!
cheers
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: NOOB guide on how to use H ?  Topic is solved

Post by HotKeyIt » 19 Nov 2020, 19:42

All you need is the AutoHotkey.exe, simply replace yours (backup your current one!).
If you don't know which one to use, start with the one that does not require vc runtime: 32-bit MT or 64-bit MT.
From there all you need for multi-threading is AhkThread function:

Code: Select all

ahk1:=AhkThread("MsgBox thread 1")
ahk2:=AhkThread("MsgBox thread 2")
MsgBox main thread
Read in v1 or v2 docs about the methods you can use with threads, like ahkgetvar, ahkFunction, ahkassign and so on.

Further you should use CriticalObject to share data between threads:

Code: Select all

obj := CriticalObject() ; Create new critical object
Loop 4 ; Create 4 Threads.
 AhkThread%A_Index% := AhkThread("obj:=CriticalObject(" (&obj) ")`nLoop`nobj[" A_Index "]:= A_Index")
 
Loop ; Show current content of object.
 ToolTip % obj.1 "`n" obj.2 "`n" obj.3 "`n" obj.4
Esc::ExitApp
User avatar
Kintaro-OEx
Posts: 12
Joined: 17 Apr 2018, 15:48

Re: NOOB guide on how to use H ?

Post by Kintaro-OEx » 19 Nov 2020, 21:40

HotKeyIt wrote:
19 Nov 2020, 19:42
All you need is the AutoHotkey.exe, simply replace yours (backup your current one!).
If you don't know which one to use, start with the one that does not require vc runtime: 32-bit MT or 64-bit MT.
From there all you need for multi-threading is AhkThread function:

Code: Select all

ahk1:=AhkThread("MsgBox thread 1")
ahk2:=AhkThread("MsgBox thread 2")
MsgBox main thread
Read in v2 docs about the methods you can use with threads, like ahkgetvar, ahkFunction, ahkassign and so on.

Further you should use CriticalObject to share data between threads:

Code: Select all

obj := CriticalObject() ; Create new critical object
Loop 4 ; Create 4 Threads.
 AhkThread%A_Index% := AhkThread("obj:=CriticalObject(" (&obj) ")`nLoop`nobj[" A_Index "]:= A_Index")
 
Loop ; Show current content of object.
 ToolTip % obj.1 "`n" obj.2 "`n" obj.3 "`n" obj.4
Esc::ExitApp
Cheers mate! I also got the compiler working properly.

Can you give me a Headstarter with a simple script with 2 THREADS. THREAD_A does press "a" constantly and THREAD_B "b" for the matter.

From there I am sure that I can move on "easily".

In any case. Thank you very much!

EDIT 00:
I got the following working but I honestly do not know how "exactly" it works with the "function call".
Am I able to separate the function definition from the "call" in AhkThread ?

Code: Select all

__THREAD_A := AhkThread("
(
    Loop
    {
		SendInput {a}
		sleep 750
    }
)", "")

__THREAD_B := AhkThread("
(
    Loop
    {
		SendInput {b}
		sleep 500
    }
)", "")
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: NOOB guide on how to use H ?

Post by HotKeyIt » 20 Nov 2020, 03:19

Don't forget #Persistent for your main thread, otherwise it will exit the program.
Yes you are able to separate them but it affects performance and you need a reference to be able to call it

Code: Select all

#Persistent
__THREAD_A := AhkThread("
(
    #Persistent
    fun(a){
     MsgBox `% ""From Thread:``n"" a
    }
)")

__THREAD_B := AhkThread("
(
    exe:=AhkExported()
    __THREAD_A := Object(" (&__THREAD_A) ")
    exe.ahkFunction(""fun"",""Hello World!"")
    __THREAD_A.ahkFunction(""fun"",""Hello World!"")
)")

fun(a){
  MsgBox % "From Main:`n" a
 }
User avatar
Kintaro-OEx
Posts: 12
Joined: 17 Apr 2018, 15:48

Re: NOOB guide on how to use H ?

Post by Kintaro-OEx » 20 Nov 2020, 08:08

HotKeyIt wrote:
20 Nov 2020, 03:19
Don't forget #Persistent for your main thread, otherwise it will exit the program.
Yes you are able to separate them but it affects performance and you need a reference to be able to call it

Code: Select all

#Persistent
__THREAD_A := AhkThread("
(
    #Persistent
    fun(a){
     MsgBox `% ""From Thread:``n"" a
    }
)")

__THREAD_B := AhkThread("
(
    exe:=AhkExported()
    __THREAD_A := Object(" (&__THREAD_A) ")
    exe.ahkFunction(""fun"",""Hello World!"")
    __THREAD_A.ahkFunction(""fun"",""Hello World!"")
)")

fun(a){
  MsgBox % "From Main:`n" a
 }

Oh man you are great :-D.

You "accidently" answered my follow-up question/issue.

With the following part I am getting the last 20 lines of a logfile/textfile. The same example is working fine outside of AhkThread.
While using it with AhkThread it obviously shows me "The following variable name contains an illegal character": namely ' " ' .

Code: Select all

USUAL AHK SCRIPT:
Last20Lines := InStr(FileContents, "`n" , false, -1, 20)

Code: Select all

AhkThread SCRIPT:
Last20Lines := InStr(FileContents, ""``n"" , false, -1, 20)
Simple question:
Why double " `` " . I do understand the double " " " part, for example ""simple text"" would work in a regular expression.

cheers mate!
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: NOOB guide on how to use H ?

Post by HotKeyIt » 20 Nov 2020, 10:20

See difference:

Code: Select all

MsgBox % "a`nb"
MsgBox % "a``nb"
So if you use MsgBox % "a`nb" you will have not valid code!

Code: Select all

MsgBox "a
b"
SOTE
Posts: 1211
Joined: 15 Jun 2015, 06:21

Re: NOOB guide on how to use H ?

Post by SOTE » 20 Nov 2020, 11:06

HotKeyIt wrote:
19 Nov 2020, 19:42
Read in v2 docs about the methods you can use with
HotKeyIt, I think a bit of confusion is caused by information being in the AutoHotkey_H v2.chm, when many users are using AutoHotkey_H v1 and coming from AutoHotkey_L v1. In addition, AutoHotkey_L v2 is still an experimental alpha (from which AutoHotkey_H v2 is merged with), so subject to more script breaking changes.

If AutoHotkey_H v2.chm is to be used as an unified help file for both v1 and v2, then perhaps this should be more clearly clarified, so that v1 users will be less confused.
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: NOOB guide on how to use H ?

Post by HotKeyIt » 20 Nov 2020, 13:42

I see.
I will take a look and either create a v1 chm or make a unified version so it can be used for both and will have examples for both, v1 and v2.
SOTE
Posts: 1211
Joined: 15 Jun 2015, 06:21

Re: NOOB guide on how to use H ?

Post by SOTE » 07 Dec 2020, 21:04

HotKeyIt wrote:
20 Nov 2020, 13:42
I see.
I will take a look and either create a v1 chm or make a unified version so it can be used for both and will have examples for both, v1 and v2.
Either way would be nice. It would be good to have clarification as to which extra features belong to which version of AHK_H.
User avatar
hasantr
Posts: 770
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: NOOB guide on how to use H ?

Post by hasantr » 24 Jan 2021, 04:34

Thanks to the information here, I finally managed to get an idea of ​​how things work. Actually it was very simple. But it's a little hard to understand.
Thank you.
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: NOOB guide on how to use H ?

Post by HotKeyIt » 24 Jan 2021, 06:50

SOTE wrote:
07 Dec 2020, 21:04
Either way would be nice. It would be good to have clarification as to which extra features belong to which version of AHK_H.
I have added v1 docs, also available online: https://hotkeyit.github.io/v1/docs/AutoHotkey.htm
User avatar
hasantr
Posts: 770
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: NOOB guide on how to use H ?

Post by hasantr » 24 Jan 2021, 07:22

What should I do to kill a thread.

When a Thread is killed, is all the ram it uses back? Or will the leaks remain in the ram as long as the main process survives?

Some operations inevitably lead to leaks. The ram cannot be recovered unless the process is terminated. Is it possible to do this with threads?

I'm sorry to change the subject. But I didn't want to make it crowded with an extra subject.
HotKeyIt
Posts: 2251
Joined: 29 Sep 2013, 18:35
Contact:

Re: NOOB guide on how to use H ?

Post by HotKeyIt » 24 Jan 2021, 15:04

If you end thread properly (see ahkTerminate), there will be no leaks, however if you kill thread without letting it free resources, e.g. dll.ahkTerminate(-1), it will lead to leaks.
SOTE
Posts: 1211
Joined: 15 Jun 2015, 06:21

Re: NOOB guide on how to use H ?

Post by SOTE » 24 Jan 2021, 15:26

HotKeyIt wrote:
24 Jan 2021, 06:50
SOTE wrote:
07 Dec 2020, 21:04
Either way would be nice. It would be good to have clarification as to which extra features belong to which version of AHK_H.
I have added v1 docs, also available online: https://hotkeyit.github.io/v1/docs/AutoHotkey.htm
Looks good, and thank you for the update.
Post Reply

Return to “AutoHotkey_H”