-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_threadFunc.ahk
56 lines (56 loc) · 2.45 KB
/
class_threadFunc.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class threadFunc {
boundParams:=[] ; Holds bound parameters. Set via bind() method.
retVals:=[] ; Holds return values until properly returned.
retId:=0 ; For tracking positions in retVals.
; User methods
__new(fn,boundParams:="",threadSettings:="",failRet:="",mute:=true){
this.fn:= IsObject(fn) ? fn : Func(fn) ; No check for isFunc, want to be able to pass bound func.
this.bind(boundParams*)
this.setThreadSettings(threadSettings)
this.failRet:=failRet ; Specify what to return if the new thread fails.
this.mute:=mute ; Set to true to suppress exceptions. True is default.
}
call(params*){
local
retId:=++this.retId ; Increment the retId.
el:=ErrorLevel ; Save the errorlevel for the calling thread.
success:=DllCall(rc:=callbackcreate(this.newThread.bind(this, params, retId),,0)) ; Call in a new thread.
callbackfree(rc) ; Free memory.
ErrorLevel:=el ; Restore the calling threads errorlevel.
if success
ret:=this.retVals[retId], this.retVals.Delete(retId) ; Fetch the return value, and remove it from the array.
return success ? ret : this.failRet ; Return it.
}
bind(params*){
return this.boundParams := params ; These params will be passed first when "this" threadFunc is called
}
setThreadSettings(set:=""){
local
this.settingFuncs:=""
if (set="") ; thread settings cleared.
return
this.settingFuncs:=[]
for fn, params in set { ; Make array of func objects to call in the new thread.
params:=IsObject(params)?params:StrSplit(params,",")
fn:=IsObject(fn)?fn:func(fn) ; Mostly intended to fit v2.
this.settingFuncs[fn]:=params ; Keep all f/bf objects in the settingFuncs arrays.
}
return
}
; Internal methods
newThread(params, retId){
local
; This is in a new thread.
; Prepare thread settings
if this.settingFuncs
for sFn, sParams in this.settingFuncs
sFn.call(sParams*) ; Call the settings function.
; Prepare call parameters
combinedParams:=[] ; For combining bound params and "call" params.
if this.boundParams.length()
combinedParams.Push(this.boundParams*) ; First bound params.
combinedParams.Push(params*) ; The "call" params.
this.retVals[retId]:=this.fn.call(combinedParams*) ; Call the function and store the return in this.retVals[retId].
return true ; Return true to indicate success.
}
}