Pseudo-Multithread a function from object class possible? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
GothicIII
Posts: 20
Joined: 17 May 2022, 04:16

Pseudo-Multithread a function from object class possible?

Post by GothicIII » 25 May 2023, 03:03

Hello,

So I'm writing streamdeck plugins and they are each combined into one library since I don't want to spawn a process for each one.

Everything works as expected but I am having issues when one plugin enters a cycle state e.g. runwait() or loop() etc.
This causes the other plugins to freeze. Any input is buffered and executed AFTER the script returns to the main function loop.

The issue here is that I am using a js websocket to connect to the streamdeck API. For this I am creating an object to execute functions from a class.
The received event data gets interpreted and then the corresponding functions are called to communicate with the streamdeck api (and other interfaces).
All function calls seem to be same-threaded if they are called from inside the OnMessage() function that resides in the Websocket object.

But this websocket object has to persist until streamdeck decides to close the websocket.
I can't use multiple websocket connections for one AHK instance unless I split up the library and create for each plugin a seperate streamdeck-Plugin environment (consists of its own folder structure with files and ressources). I'd rather have everything in one place.


To summerize with pseudocode:

Code: Select all


Parse A_Args
class websocket{
	__New(A_Args){ connect to websocket and create neccessary objects }
	function1(){}
	function2(){}
	...
	OnMessage(event_data){
	if event_data='bob'
		streamdeck_plugin_one()
	if event_data='alice'
		streamdeck_plugin_one()
	}
	Send(data)
	{
		this.send_back_to_websocket(data)
	}
}

streamdeck_plugin_one(){
	do things which take a long time
	obj.send(data:=whatever_is_needed)
}

streamdeck_plugin_two(){
	do things
	obj.send(data:=whatever_is_needed)
}
obj:=websocket(A_Args)
streamdeck_plugin_two() is only executed after the 'things which take a long time' from streamdeck_plugin_one() are finished

Is it possible to somehow pseudo-mutithread OnMessage() ?
Or do I have to split every plugin into its own folder structure and manage multiple websocket instances with one ahk process?

EDIT: I think it works if I use multiple script instances. Very complicated though. I need to break apart the plugin and websocket classes.
When a websocket.OnMessage event happens, I'd do a run() with parameters so the plugin part can do its job.
To coordinate the tasks I'd have to use OnMessage() on the main thread and Sendmessage() on the plugin-thread. I chose WM_COPYDATA to send messages back and forth. I'd need to pass at least strings.
With this I can also interrupt current threads which is quiet nice. But god is it complicated :/

GothicIII
Posts: 20
Joined: 17 May 2022, 04:16

Re: Pseudo-Multithread a function from object class possible?  Topic is solved

Post by GothicIII » 25 May 2023, 16:46

Puuh. Spend whole day to get this working. I am not a programming expert so I didn't know it was such an obvious way.
I refractured the code and seperated the whole plugin logic into a second script. The main script only send/receives websocket events and executes the plugin script if required with run().
The plugin script can be run on multiple instances and sends WM_COPYDATA messages back to the main script which contain neccessary json strings to communicate with the stream deck api. This was my luck that it was only a null-terminated, fixed size string.
If I'd had to send objects or structs I'd have no idea how to do that...

Post Reply

Return to “Ask for Help (v2)”