AHK_H: how to create an alias to a function from another thread?

Ask for help, how to use AHK_H, etc.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

AHK_H: how to create an alias to a function from another thread?

28 Nov 2016, 04:34

You can create aliases to variables from another thread with:
alias(aliasVar, threadObj.ahkgetvar('varName',true))

Can this be done with function as well?

I'd like to avoid using the syntax %funcRef%(params)
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: AHK_H: how to create an alias to a function from another thread?

28 Nov 2016, 05:09

More generally, the question is whether it's possible to shorten the expression:
thread.ahkFunction("func_name",params*)
into something like:
func_name(params*)
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: AHK_H: how to create an alias to a function from another thread?

28 Nov 2016, 08:26

You can refer to this script, which requires H2.

Code: Select all

f:=ahkFast("#Persistent`na:=[1,2,3]`ntest(s){`nreturn s`n}")
Sleep, 200
f.set("test","2")
MsgBox %  f.a.1 " " f.test " " f.test([1,2,"c"]).3



ahkFast(this:="",code:="",wait:=""){
	static dll
	if isobject(this)
	{
		s:="__default_Created(){`nstatic _:=__default_Created(),Thread`n"
			. "Thread:=CriticalObject(" (&this) "),Thread.base:={__Call:CriticalObject(Func(`"__default_call`")),__set:CriticalObject(Func(`"__default_set`")),__get:CriticalObject(Func(`"__default_get`"))}`n"
			. "}`n__default_get(f,n,p:=`"`"){`nglobal`nTry`nreturn (p?`%n`%[p]:`%n`%)`n}`n"
			. "__default_set(f,n,p){`nglobal`nTry`nreturn (`%n`%:=p)`n}`n"
			. "__default_call(f,n,p*){`n`nif n:=func(n)`nreturn p.Length()?n.call(p*):n.call()`n}`n" code
		n:=this.new,n[s]
	}
	else
	{
		if !dll
			UnZipRawMemory(LockResource(LoadResource(0,ad:=FindResource(0,"F903E44B8A904483A1732BA84EA6191F",10))),SizeofResource(0,ad),dll)
		f:=CriticalObject(),f[""]:=r:=MemoryLoadLibrary(&dll),f.fast:=func("ahkFast")
		for i,n in {"fn":["ahkFunction","s==sssssssssss"],"fp":["ahkPostFunction","i==sssssssssss"],"new":["ahktextdll","ut==sss"]
			,"add":["addScript","ut==si"],"exec":["ahkExec","ut==sui"],"get":["ahkgetvar","s==sui"],"set":["ahkassign","ui==ss"],"sub":["ahkLabel","ui==sui"]
			,"pause":["ahkPause","i==s"],"end":["ahkTerminate","i==i"],"Ready":["ahkReady",""],"Reload":["ahkReload","i==i"]}
			f[i]:=DynaCall(MemoryGetProcAddress(r,n.1),n.2)
		if this!=""
			ahkFast(f,this,code)
		return f
	}
}
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: AHK_H: how to create an alias to a function from another thread?

28 Nov 2016, 09:09

Wow, that's some ugly soup of code.
I guess I'll just do this instead:

Code: Select all

global THREAD
func_name(params*){
	return THREAD.ahkFunction('func_name',params*)
}
Much simpler.
Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: AHK_H: how to create an alias to a function from another thread?

28 Nov 2016, 13:35

Would it be possible to use the pointer of a function as returned by ahkFindFunc to create an alias?
Example:

Code: Select all

myFunc := THREAD.ahkFindFunc('myFunc')
myFunc(params) ; <--- how to turn myFunc into a callable?
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: AHK_H: how to create an alias to a function from another thread?

29 Nov 2016, 09:41

it would be nice if AHK_H had its own thread in Scripts & Functions (like it used to in the old forum), so that we could post questions directly there and HotKeyIt wouldn't miss them

Getfree
Posts: 231
Joined: 12 Oct 2014, 18:00

Re: AHK_H: how to create an alias to a function from another thread?

29 Nov 2016, 09:57

Who decides if AHK_H gets it's own thread?
Can we create one?
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AHK_H: how to create an alias to a function from another thread?

29 Nov 2016, 12:02

In the child thread, you would do something like this:

global SomeVar := ObjShare(Func("SomeFunc"))

The use something like this in the main thread to get the reference to the function:

this.MyThreadFunc := ObjShare(MyThread.ahkgetvar("SomeVar"))

This will give you a thread-safe way to call a function from a child thread

You can see an implementation of stuff like this in UCR.

Main thread: https://github.com/evilC/UCR/blob/maste ... le.ahk#L86
Child Thread: https://github.com/evilC/UCR/blob/maste ... ad.ahk#L36
Last edited by evilC on 29 Nov 2016, 12:05, edited 2 times in total.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: AHK_H: how to create an alias to a function from another thread?

29 Nov 2016, 12:03

Getfree wrote:Who decides if AHK_H gets it's own thread?
Can we create one?
imo, HotKeyIt should decide, since its his project and he would be answering the questions

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

Re: AHK_H: how to create an alias to a function from another thread?

29 Nov 2016, 18:15

The alias can be also created using DynaCall:

Code: Select all

thread:=AhkThread("#Persistent`nfun(a*){`nMsgBox `% a.1 a.2 a.3`nreturn `"hello`"`n}")
fun:=DynaCall(MemoryGetProcAddress(thread[""],"ahkFunction"),["s==sssssssssss",2,3,4,5,6,7,8,9,10,11],"fun")
MsgBox % fun["Auto","Hot","Key"]
______________________________________________________________________
I don't mind having separate thread for AHK_H.
Though I am not sure if I can decide, I think lexikos, tank and joedf need to agree.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: AHK_H: how to create an alias to a function from another thread?

29 Nov 2016, 19:48

HotKeyIt wrote: I don't mind having separate thread for AHK_H.
Though I am not sure if I can decide, I think lexikos, tank and joedf need to agree.
anyone can create a thread in Scripts & Functions forum for their own project. you had one in the old forum

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

Re: AHK_H: how to create an alias to a function from another thread?

30 Nov 2016, 03:08

Sorry, I mean forum section, not thread.
Thread will get quickly full and messy and people would still miss it and post else where.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: AHK_H: how to create an alias to a function from another thread?

30 Nov 2016, 09:44

HotKeyIt wrote:Sorry, I mean forum section, not thread.
ah yes, an entire forum section for AHK_H might need approval. i think its overkill as well. i dont think AHK_H gets enough questions to really warrant its own forum section.
HotKeyIt wrote:Thread will get quickly full and messy and people would still miss it and post else where.
even if AHK_H had its own section, people would still miss it and post else where. so the problem is likely the same. all posts could get merged into your Scripts & Functions thread. i think its better to have SOMETHING for now, where at least some people can go to for questions, and a place where you can easily find questions


Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 27 guests