Just started with _H v2... but.. where do I start??? Topic is solved

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Just started with _H v2... but.. where do I start???

30 Mar 2019, 06:56

Hello! I'm excited as I just downloaded AHK_H v2 :D

I went to run a simple MsgBox and received this error warning :?

............ okay now I can't recreate it but it said something about a function not being able to be called?? hmm..

Where can I better understand how things work in AHK_H v2? I'm not sure what to learn first - obviously my goal is multi-threading, but I just want to start of with basics (one of those basics being an AHK_H intro to multi-threading).

From my understanding, I thought that a script would be interpretted extremely similarly to how AHK_L v2 does.. but maybe that's not the case? :P

Idk maybe after I experiment more then I will see that maybe I was just experiencing something odd..
-TL
swagfag
Posts: 2608
Joined: 11 Jan 2017, 17:59

Re: Just started with _H v2... but.. where do I start???

30 Mar 2019, 17:16

there isnt a starting point. u read everything ahkh related in the docs, u go over a bunch of forum posts and hotkeyit's responses. u pray some of that rubs off on u and the rest is up to personal experimentation

a lot of this stuff has its roots in the winapi, so u can usually read up on msdn about some things, eg thread local storage, critical sections etc

anyhoo, just post if ure stuck. its likely that whatever it is ure dealing with will get resolved faster than if u just kept mucking around on ur own instead.

.... or not, idk, im not omniscient
Last edited by swagfag on 30 Mar 2019, 20:03, edited 1 time in total.
HotKeyIt
Posts: 1850
Joined: 29 Sep 2013, 18:35
Contact:

Re: Just started with _H v2... but.. where do I start???

30 Mar 2019, 17:40

Read the docs about new features (http://hotkeyit.github.io/v2/docs/AHKH_Features.htm), everything else is normal ahk v2: https://lexikos.github.io/v2/docs/AutoHotkey.htm

Here is a simple code to get started with multithreading:

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
Here is the same code running without AutoHotkey.dll:

Code: Select all

obj := CriticalObject() ; Create new critical object
Loop 4 ; Create 4 Threads.
 ExeThread%A_Index% := ExeThread("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
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Re: Just started with _H v2... but.. where do I start???

31 Mar 2019, 01:15

swagfag wrote:
30 Mar 2019, 17:16
there isnt a starting point. u read everything ahkh related in the docs, u go over a bunch of forum posts and hotkeyit's responses. u pray some of that rubs off on u and the rest is up to personal experimentation

a lot of this stuff has its roots in the winapi, so u can usually read up on msdn about some things, eg thread local storage, critical sections etc

anyhoo, just post if ure stuck. its likely that whatever it is ure dealing with will get resolved faster than if u just kept mucking around on ur own instead.

.... or not, idk, im not omniscient
Thanks so much for the guidance y'all. I actually read almost every thread in the AHK_H forum before posting this Q - as I thought I would probably find my answer before posting this, but was still left puzzled on where to start. I am actively reading the _H v2 docs, but part of the reason I posted was because I'm confused about how to write code for each thread to execute - the _H v2 doc examples are confusing compared to v1/v2 doc examples (probably because I'm new to programming and multithreading).

I think this example actually needs to be corrected:

AhkThread docs

Code: Select all

ahkdll:=AhkThread("Msgbox `% variable:=`"Thread`"") ; Loads the AutoHotkey module and starts the script.
While !ahkdll.ahkgetvar.variable
  Sleep 50 ; wait until variable has been set.
MsgBox % ahkdll.ahkgetvar.variable ; Display content of variable in thread
ahkthread_free(ahkdll),ahkdll:="" ; Stop execution in thread and free resources.
As is, it kept throwing the error:

Error at line 1.

Line Text: % variable:="Thread")
Error: Missing ending "%"


So I removed the ` and % sign on line 1 and then it threw this error:

Missing ending "%"
Specifically: % ahkdll.ahkgetvar.variable)


So I removed the % sign on line 4, and was left with this:

Code: Select all

ahkdll:=AhkThread("Msgbox variable:=`"Thread`"") ; Loads the AutoHotkey module and starts the script.
While !ahkdll.ahkgetvar.variable
	Sleep 50 ; wait until variable has been set.
MsgBox(ahkdll.ahkgetvar.variable) ; Display content of variable in thread
ahkthread_free(ahkdll),ahkdll:="" ; Stop execution in thread and free resources.
Which appeared to work correctly - two MsgBoxes popped up, one right after the other............? Actually, I'm not really sure why two pop up, but I can see that there are two AHK threads in my Tray Menu, the newly created thread from this script is showing that it is running the MsgBox like a script normally would (Msgbox(variable:="Thread"), while the "root?" thread executes the message box as it normally would (MsgBox(ahkdll.ahkgetvar.variable)
HotKeyIt wrote:
30 Mar 2019, 17:40
Read the docs about new features (http://hotkeyit.github.io/v2/docs/AHKH_Features.htm), everything else is normal ahk v2: https://lexikos.github.io/v2/docs/AutoHotkey.htm

Here is a simple code to get started with multithreading:

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
Here is the same code running without AutoHotkey.dll:

Code: Select all

obj := CriticalObject() ; Create new critical object
Loop 4 ; Create 4 Threads.
 ExeThread%A_Index% := ExeThread("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
I see that we are creating 4 objects here, which are obv 4 thread objects.



On the AhkThread example you gave me, I tried this:

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")

obj.1(MsgBox("1"))
obj.2(MsgBox("2"))
obj.3(MsgBox("3"))
obj.4(MsgBox("4"))
A message box pops up showing "1", but then right after I get this error:

---------------------------
Multithreading Test.ahk
---------------------------
Error: Call to nonexistent function.

Specifically: 2455824

Line#
003: SetWorkingDir(A_ScriptDir)
004: SetTitleMatchMode(2)
008: obj := CriticalObject()
009: Loop "4"
010: AhkThread%A_Index% := AhkThread("obj:=CriticalObject(" (&obj) ")
Loop
obj[" A_Index "]:= A_Index")
---> 012: obj.1(MsgBox("1"))
013: obj.2(MsgBox("2"))
014: obj.3(MsgBox("3"))
015: obj.4(MsgBox("4"))
018: Return
018: ExitApp()
018: Return
019: Exit

The current thread will exit.


I'm obviously doing something wrong, but why does the msgbox still pop up, then throw an error afterwards instead of before?

I then found that this works:

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")

AhkThread1.ahkExec("MsgBox(1)")
AhkThread2.ahkExec("MsgBox(2)")
AhkThread3.ahkExec("MsgBox(3)")
AhkThread4.ahkExec("MsgBox(4)")
But multithreading is not happening, as the msgboxes pop up only after the one preceeding it is closed.

How do I tell each thread what to do and make them execute simultaneously?

For starters, for multi-threading practice I think it would be cool to try and use multi-threading to scrape web data from 4 web addresses at the same time.

Here is a simple code using IE COM:

Code: Select all

		wb := ComObjCreate("InternetExplorer.Application")
	,	wb.Navigate("https://www.autohotkey.com")
		While ((wb.readyState != 4) || (wb.document.readyState != "complete") || (wb.busy))
			Sleep(10)
		URL := wb.document.url
	,	titleTag := wb.document.title
	,	MsgBox("URL: " URL "`n`nTitle Tag: " titleTag)
	,	wb := ""
How would I run the code above to simultaneously execute this code on four different web addresses?

I tried this but Keep getting an error on the lines with while in it:
The following reserved word must not be used as a variable name:
"While"

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")

AhkThread1.ahkExec(wb := ComObjCreate("InternetExplorer.Application"))
AhkThread1.ahkExec(wb.Navigate("https://www.autohotkey.com"))
AhkThread1.ahkExec("While ((wb.readyState != 4) || (wb.document.readyState != "complete") || (wb.busy))"
					Sleep(10))
AhkThread1.ahkExec(URL := wb.document.url)
AhkThread1.ahkExec(titleTag := wb.document.title)
AhkThread1.ahkExec(MsgBox("URL: " URL "`n`nTitle Tag: " titleTag))
AhkThread1.ahkExec("while WinExist("ahk_class IEFrame")"
					Process, Close, iexplore.exe)
AhkThread1.ahkExec(wb := "")
so I put quotes around the lines with while like so, but got another error:
Missing space or operator before this.
Specifically: complete") || (wb.busy))" Sleep(10))

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")

AhkThread1.ahkExec(wb := ComObjCreate("InternetExplorer.Application"))
AhkThread1.ahkExec(wb.Navigate("https://www.autohotkey.com"))
AhkThread1.ahkExec("While ((wb.readyState != 4) || (wb.document.readyState != "complete") || (wb.busy))"
					Sleep(10))
AhkThread1.ahkExec(URL := wb.document.url)
AhkThread1.ahkExec(titleTag := wb.document.title)
AhkThread1.ahkExec(MsgBox("URL: " URL "`n`nTitle Tag: " titleTag))
AhkThread1.ahkExec("while WinExist("ahk_class IEFrame")"
					Process, Close, iexplore.exe)
AhkThread1.ahkExec(wb := "")
I don't understand when or why I need to wrap certain things in quotations and certain things not?

For example, this works:

Code: Select all

AhkThread1.ahkExec("MsgBox(1)")
and this works initally but after displaying the message box, throws an error:

Code: Select all

AhkThread2.ahkExec(MsgBox(2))
---------------------------
Multithreading Test.ahk
---------------------------
Error in #include file ""C:\Program Files\AutoHotkey\AutoHotkey.exe"":
Call to nonexistent function.

Specifically: OK()

Line#
---> 001: OK()
002: Exit

The program will exit.


This doesn't work, throws an error:
Missing close-quote
Specifically: ")

Code: Select all

AhkThread1.ahkExec("wb := ComObjCreate("InternetExplorer.Application")")
This works appears to create a new Internet Explorer instance, however I can't make it navigate anywhere or make it visible.

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")

AhkThread1.ahkExec(wb := ComObjCreate("InternetExplorer.Application"))
AhkThread1.ahkExec(wb.Visible)
AhkThread1.ahkExec(wb.Navigate("about:blank"))
What are the pros/cons to using threads from other processes like you showed above in your second example vs. using an ahkThread?

Any help is much appreciated. Sorry if I sound dumb. :?
-TL
HotKeyIt
Posts: 1850
Joined: 29 Sep 2013, 18:35
Contact:

Re: Just started with _H v2... but.. where do I start???  Topic is solved

31 Mar 2019, 05:17

You have always to pass string to your thread, you can always check if you code looks proper via msgBox.
ahkExec only executes some code and deletes it afterwards, main exe is waiting for execution to finish so it can tidy up the code.
ahktextdll starts a new script from string, AhkThread does it internally too:

Code: Select all

Loop 4 ; Create 4 Threads.
	AhkThread%A_Index% := AhkThread("MsgBox(" A_Index ")"")
MsgBox "Press ok to exit"
You should better use a proper script and control it rather than using ahkExec:

Code: Select all

script:="
(`
wb := ComObjCreate("InternetExplorer.Application")
	,	wb.Navigate(A_Args.1)
		While ((wb.readyState != 4) || (wb.document.readyState != "complete") || (wb.busy))
			Sleep(10)
		URL := wb.document.url
	,	titleTag := wb.document.title
	,	MsgBox("URL: " URL "`n`nTitle Tag: " titleTag)
	,	wb := ""
)"
thread1:=AhkThread(script,"https://www.autohotkey.com")
thread2:=AhkThread(script,"https://www.google.com")
While thread1.ahkReady || thread2.ahkReady
  Sleep 100
In my second example I showed that you can start a new thread inside the exe, not another process.
All parameters here need to be strings:
AhkThread() will start a new empty thread, same as AhkThread("#Persistent")

Code: Select all

AhkThread1.ahkExec("wb := ComObjCreate(`"InternetExplorer.Application`")")
AhkThread1.ahkExec("wb.Visible")
AhkThread1.ahkExec("wb.Navigate(`"about:blank`")")
User avatar
Tigerlily
Posts: 283
Joined: 04 Oct 2018, 22:31

Re: Just started with _H v2... but.. where do I start???

31 Mar 2019, 20:00

HotKeyIt wrote:
31 Mar 2019, 05:17
WOW - this is so cool HotKeyIt!! I've been wanting to use AHK_H since I found out about it, but didn't know where to start. This will help get me off the ground, and I am extremely grateful for you and your time!

Thanks for making such an amazing AHK fork - it's definitely a must-have for any serious AutoHotkey programmer (or any programmer for that matter).

If I make anything worth sharing, I may post in the _H board so maybe others who are lost like myself have some more material to get their engine started.

Once again - thanks a million!!

stoked. :bravo:
-TL

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 3 guests