Please help me create a basic multi-threaded example Topic is solved

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
micasa
Posts: 24
Joined: 29 Dec 2018, 04:21

Please help me create a basic multi-threaded example

02 Jan 2019, 04:18

Hello, everyone.

I have seen the forum post entitled [AHK.dll] Multi-Threading Basic Examples:
https://autohotkey.com/board/topic/7586 ... les/page-1

But the examples there were still too complicated for me :headwall:

Let's say that I have a very simple single-threaded AHK V2 script as follows:

Code: Select all

data := 'data for API call'
result := a(data)
result .= b(data)
result .= c(data)
result .= d(data)
MsgBox(result)

a(data)
{
	HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	; DO STUFF
	Return(result)
}

b(data)
{
	HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	; DO STUFF
	Return(result)
}

c(data)
{
	HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	; DO STUFF
	Return(result)
}

d(data)
{
	HttpObj := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	; DO STUFF
	Return(result)
}
What would be the simplest way to convert this to a multi-threaded script that uses 4 threads, one for each function call?

Any code examples would be greatly appreciated.

Thank you in advance for your time and consideration.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Please help me create a basic multi-threaded example  Topic is solved

02 Jan 2019, 07:56

Create 4 files that have the code you want to execute.
Then use following main script:

Code: Select all

dll1:=AhkThread("Alias(a," getvar(a) "`n#include ScriptA.ahk")
dll2:=AhkThread("Alias(b," getvar(b) "`n#include ScriptB.ahk")
dll3:=AhkThread("Alias(c," getvar(c) "`n#include ScriptC.ahk")
dll4:=AhkThread("Alias(d," getvar(d) "`n#include ScriptD.ahk")
While dll1.ahkReady() || dll2.ahkReady() || dll3.ahkReady() || dll4.ahkReady()
	Sleep 100
MsgBox a "`n`n" b "`n`n" c "`n`n" d
micasa
Posts: 24
Joined: 29 Dec 2018, 04:21

Re: Please help me create a basic multi-threaded example

02 Jan 2019, 10:31

Thank you very much for taking the time to reply.

I kept getting the Error: Missing "}" at line 1.

I tried running it like this:

Code: Select all

data := "data for API call"
dll1:=AhkThread("Alias(a," getvar(data) "`n#include ScriptA.ahk")
dll2:=AhkThread("Alias(b," getvar(data) "`n#include ScriptB.ahk")
dll3:=AhkThread("Alias(c," getvar(data) "`n#include ScriptC.ahk")
dll4:=AhkThread("Alias(d," getvar(data) "`n#include ScriptD.ahk")
While dll1.ahkReady() || dll2.ahkReady() || dll3.ahkReady() || dll4.ahkReady()
	Sleep 100
MsgBox a "`n`n" b "`n`n" c "`n`n" d
Can you give me an example of what I need to put in ScriptA.ahk, etc. in order to manipulate variable "data"? I am using AHK_H Unicode 32-bit V2.

Sorry for taking up your precious time.
micasa
Posts: 24
Joined: 29 Dec 2018, 04:21

Re: Please help me create a basic multi-threaded example

02 Jan 2019, 10:49

I realized that the code was missing a ")".

So, this should have been:

Code: Select all

data := "data for API call"
a := data
b := data
c := data
d := data

dll1:=AhkThread("Alias(a," getvar(a) ")`n#include ScriptA.ahk")
dll2:=AhkThread("Alias(b," getvar(b) ")`n#include ScriptB.ahk")
dll3:=AhkThread("Alias(c," getvar(c) ")`n#include ScriptC.ahk")
dll4:=AhkThread("Alias(d," getvar(d) ")`n#include ScriptD.ahk")
While dll1.ahkReady() || dll2.ahkReady() || dll3.ahkReady() || dll4.ahkReady()
	Sleep 100
MsgBox(a b c d)
Then it is simply a matter of manipulating variables "a", "b", "c" and "d" in the include scripts.

Thank you for the simple solution. I got it to work now! Have a good day.

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 17 guests