AhkSelf()

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
arcticir
Posts: 542
Joined: 17 Nov 2013, 11:32

AhkSelf()

Post by arcticir » 01 Apr 2017, 13:17

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)}
}
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: AhkSelf()

Post by guest3456 » 01 Apr 2017, 13:46

What does it do?

HotKeyIt
Posts: 1696
Joined: 29 Sep 2013, 18:35
Contact:

Re: AhkSelf()

Post by HotKeyIt » 01 Apr 2017, 14:51

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({...})
scsnake
Posts: 23
Joined: 05 Aug 2015, 03:19

Re: AhkSelf()

Post by scsnake » 28 May 2017, 19:17

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)})
}
HotKeyIt
Posts: 1696
Joined: 29 Sep 2013, 18:35
Contact:

Re: AhkSelf()

Post by HotKeyIt » 28 May 2017, 19:33

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
scsnake
Posts: 23
Joined: 05 Aug 2015, 03:19

Re: AhkSelf()

Post by scsnake » 22 Jun 2017, 22:01

Is there any documentation for the "sub" function? I thought a subroutine equals to a label ??
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: AhkSelf()

Post by guest3456 » 23 Jun 2017, 01:21

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

Post Reply

Return to “AutoHotkey_H”