Page 1 of 1

AhkSelf()

Posted: 01 Apr 2017, 13:17
by arcticir
Applicable to all types of threads: exe/tls/dll

Code: Select all

AhkSelf(){
	if a_isdll
		gpa:=func("MemoryGetProcAddress"),lib:=A_MemoryModule
	else 
	{
		#DllImport, gpa,GetProcAddress,ptr,0,astr,0
		gpa:=func("gpa"),lib:=GetModuleHandle()
	}
	return {func:DynaCall(gpa.call(lib,"ahkFunction"),"s==sssssssssssui","","","","","","","","","","","",A_ThreadID)
			,post:DynaCall(gpa.call(lib,"ahkPostFunction"),"i==sssssssssssui","","","","","","","","","","","",A_ThreadID)
			,add:DynaCall(gpa.call(lib,"addScript"),"ut==siui","",0,A_ThreadID)
			,get:DynaCall(gpa.call(lib,"ahkgetvar"),"s==suiui","",0,A_ThreadID)
			,set:DynaCall(gpa.call(lib,"ahkassign"),"i==ssui","","",A_ThreadID)
			,sub:DynaCall(gpa.call(lib,"ahkLabel"),"ui==suiui","",0,A_ThreadID)
			,exec:DynaCall(gpa.call(lib,"ahkExec"),"ut==sui","",A_ThreadID)
			,pause:DynaCall(gpa.call(lib,"ahkPause"),"ui==sui","",A_ThreadID)
			,ready:DynaCall(gpa.call(lib,"ahkReady"),"ui==ui",A_ThreadID)}
}

Re: AhkSelf()

Posted: 01 Apr 2017, 13:46
by guest3456
What does it do?

Re: AhkSelf()

Posted: 01 Apr 2017, 14:51
by HotKeyIt
guest3456 wrote:What does it do?
It wraps the current thread into an object so you can use the functions from same thread.


I think if would make sense to use CriticalObject by default so other threads can use the object as well. return CriticalObject({...})

Re: AhkSelf()

Posted: 28 May 2017, 19:17
by scsnake
Can I call main thread's label/function from subthread? The following script fails to show the msgbox :?:

Code: Select all

ahkself:=ahkSelf()

sub:=ahkmini("
(
#persistent
AhkSelf:=CriticalObject(" (&ahkself) ")
return
sub:
ahkself.ahkLabel(""main"")
return
)")
while !sub.ahkReady()
	Sleep 500

sub.ahkLabel("sub")
return

main:
MsgBox Main
return

AhkSelf(){
	if a_isdll
		gpa:=func("MemoryGetProcAddress"),lib:=A_MemoryModule
	else 
	{
		#DllImport, gpa,GetProcAddress,ptr,0,astr,0
		gpa:=func("gpa"),lib:=GetModuleHandle()
	}
	return Criticalobject({func:DynaCall(gpa.call(lib,"ahkFunction"),"s==sssssssssssui","","","","","","","","","","","",A_ThreadID)
			,post:DynaCall(gpa.call(lib,"ahkPostFunction"),"i==sssssssssssui","","","","","","","","","","","",A_ThreadID)
			,add:DynaCall(gpa.call(lib,"addScript"),"ut==siui","",0,A_ThreadID)
			,get:DynaCall(gpa.call(lib,"ahkgetvar"),"s==suiui","",0,A_ThreadID)
			,set:DynaCall(gpa.call(lib,"ahkassign"),"i==ssui","","",A_ThreadID)
			,sub:DynaCall(gpa.call(lib,"ahkLabel"),"ui==suiui","",0,A_ThreadID)
			,exec:DynaCall(gpa.call(lib,"ahkExec"),"ut==sui","",A_ThreadID)
			,pause:DynaCall(gpa.call(lib,"ahkPause"),"ui==sui","",A_ThreadID)
			,ready:DynaCall(gpa.call(lib,"ahkReady"),"ui==ui",A_ThreadID)})
}

Re: AhkSelf()

Posted: 28 May 2017, 19:33
by HotKeyIt
Note the function is sub here, not ahkLabel!

Code: Select all

ahkself:=ahkSelf()

sub:=ahkThread("
(
#persistent
AhkSelf:=CriticalObject(" (&ahkself) ")
return
sub:
ahkself.sub(""main"")
return
)")
while !sub.ahkReady()
	Sleep 500
sub.ahkLabel("sub")
return

main:
MsgBox Main
return

Re: AhkSelf()

Posted: 22 Jun 2017, 22:01
by scsnake
Is there any documentation for the "sub" function? I thought a subroutine equals to a label ??

Re: AhkSelf()

Posted: 23 Jun 2017, 01:21
by guest3456
scsnake wrote:Is there any documentation for the "sub" function? I thought a subroutine equals to a label ??
the "documentation" is within the AHKSelf() function. it returns an object with a .sub() method which calls ahkLabel