Is it possible to share class objects with autohotkey.dll?

Ask for help, how to use AHK_H, etc.
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Is it possible to share class objects with autohotkey.dll?

10 Jul 2015, 16:03

I have a Gui Class that I wrote for my application, in my app I have a main gui and I want to create another gui in it's own thread. Creating the gui in it's own thread is not a problem, but how do I control the secondary Gui from my main thread?

this does not work

Code: Select all

#Include <class_UI>

gui := object()
GlobalVarsScript( "gui", 0, gui )
thread := AhkThread( GlobalVarsScript() "`n#Include <class_UI>`ngui := new UI( ""-DPIScale"", ""test"" )`ngui.Show( ""x10 y10 w500 h500"" )`nreturn" )
sleep 3000
gui.Hide()
I really didn't expect it to work, it was more like I hoped it would work. Is there anyway to accomplish what I am trying to do?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Is it possible to share class objects with autohotkey.dll?

12 Jul 2015, 13:35

There was a problem with Sleep which is now fixed, update AutoHotkey_H and following example will work, thanks for reporting btw.

When you assign gui := in your thread, you replace the object you shared to dll so this will not work, instead use Alias in your main thread.
Also your thread needs to use SetBatchLines, -1 otherwise the call will not return as it will do internal MsgSleep forever.

Code: Select all

thread := AhkThread("#Include <class_UI>`nSetBatchLines,-1`ngui := new UI( ""-DPIScale"", ""test"" )`ngui.Show( ""x10 y10 w500 h500"" )`nready:=1`nreturn" )
While !thread.ahkgetvar("ready")
	Sleep 100
Sleep 500
Alias(gui,thread.ahkgetvar("gui",1))
gui.Hide()
MsgBox End
If you want your object to be CriticalObject (thread-safe), pass object pointer of gui to CriticalObject() in main thread:

Code: Select all

thread := AhkThread("#Include <class_UI>`nSetBatchLines,-1`ngui := CriticalObject(new UI( ""-DPIScale"", ""test"" ))`ngui.Show( ""x10 y10 w500 h500"" )`nguiptr:=&gui`nreturn" )
While !thread.ahkgetvar("guiptr")
	Sleep 100
Sleep 500
gui:=CriticalObject(thread.ahkgetvar("guiptr")+0)
gui.Hide()
MsgBox End
ExitApp
User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: Is it possible to share class objects with autohotkey.dll?

13 Jul 2015, 02:03

Thanks HotKeyIt

Would you, or anyone for that matter, explain/show examples of when I should use Alias() and when I should use CriticalObject()? I did not realize I could use Alias() to retrieve an object.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Is it possible to share class objects with autohotkey.dll?

13 Jul 2015, 06:10

Alias is similar to ByRef to share a variable, CriticalObject() is only sharing the object and CriticalSection to make sure the object is accessed from one thread only.
You need to use CriticalObject whenever you access the object from multiple threads.

You can also Alias a variable that holds a CriticalObject:

Code: Select all

thread := AhkThread("#Include <class_UI>`nSetBatchLines,-1`ngui := CriticalObject(new UI( ""-DPIScale"", ""test"" ))`ngui.Show( ""x10 y10 w500 h500"" )`nready:=1`nreturn" )
While !thread.ahkgetvar("ready")
	Sleep 100
Sleep 500
Alias(gui,thread.ahkgetvar("gui",true))
gui.Hide()
MsgBox End
Once CriticalObject is created, you must not replace it, otherwise an access violation can happen.

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 52 guests