Hello! I'm excited as I just downloaded AHK_H v2
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?
Idk maybe after I experiment more then I will see that maybe I was just experiencing something odd..
Just started with _H v2... but.. where do I start??? Topic is solved
Re: Just started with _H v2... but.. where do I start???
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
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.
Re: Just started with _H v2... but.. where do I start???
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:
Here is the same code running without AutoHotkey.dll:
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
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
Re: Just started with _H v2... but.. where do I start???
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).swagfag wrote: ↑30 Mar 2019, 17:16there 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
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.
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.
I see that we are creating 4 objects here, which are obv 4 thread objects.HotKeyIt wrote: ↑30 Mar 2019, 17:40Read 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:Here is the same code running without AutoHotkey.dll: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
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
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"))
---------------------------
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)")
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 := ""
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 := "")
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 := "")
For example, this works:
Code: Select all
AhkThread1.ahkExec("MsgBox(1)")
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")")
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"))
Any help is much appreciated. Sorry if I sound dumb.
-TL
Re: Just started with _H v2... but.. where do I start??? Topic is solved
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:
You should better use a proper script and control it rather than using ahkExec:
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")
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"
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
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`")")
Re: Just started with _H v2... but.. where do I start???
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.
-TL
Who is online
Users browsing this forum: No registered users and 5 guests