Thread limitation

Get help with using AutoHotkey and its commands and hotkeys
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Thread limitation

17 Oct 2018, 13:52

Hello

In my script I make a DllCall which takes a while, maybe a few seconds before it returns from the DllCall, possibly up to 6 seconds on some systems. I must call this function at every startup of my script. During this time I cannot update a progress bar to let the user know it's loading, or even do anything else for that matter, because the DllCall is happening in a single line of code, and single lines of code can't be interrupted by another thread before the line is finished, it seems. Is there any way around this? Is there some WinAPI function that can force that DllCall to a separate thread from the OS perspective so it can keep allocating time slices to my other main thread?

Thanks
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Thread limitation

17 Oct 2018, 14:44

There are other ways to do this, but what you could do it has a small second script to run the DllCall then return the data once it is finish, that way your main script is still going through what it wants then can wait for the DllCall to finish.
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Thread limitation

18 Oct 2018, 01:01

MannyKSoSo wrote:
17 Oct 2018, 14:44
There are other ways to do this, but what you could do it has a small second script to run the DllCall then return the data once it is finish, that way your main script is still going through what it wants then can wait for the DllCall to finish.
Yeah I guess that could work, and send a window message to the original script when it's done.

I'd be interested in those other ways though, do they involve using this function?
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Thread limitation

20 Oct 2018, 03:23

So it seems CreateThread can work, but it might create some really weird bugs according to lexikos:

https://autohotkey.com/boards/viewtopic ... ad#p117223

Still tempted to use it as my use case scenario is simple (just one line of code) and only does it once at program startup.

But no, better not! :lol:
DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: Thread limitation

20 Oct 2018, 07:42

Lexikos said: Run one script to do the background stuff (e.g. file I/O) and a separate script to interface with the user (hotkeys, menus, GUIs, etc.). Do this either with separate script processes or with AutoHotkey.dll/AutoHotkey_H

So if I get it right? Each script since they are unique processes they will use their own threads?
So by having multiple scripts run different tasks we would have a pseudo multi threaded global script when they interact all together??? :)
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Thread limitation

20 Oct 2018, 09:15

DRocks wrote:
20 Oct 2018, 07:42
So by having multiple scripts run different tasks we would have a pseudo multi threaded global script when they interact all together??? :)

Yes but it's stupid because your end users don't want to have all these dumb processes spawning in the background triggering antivirus to stop and ask for permission every time and leave the user thinking what the hell this programmer was thinking :lol:

And then you have to hope that window messages are working properly on the end user's machine, i.e Windows Security Policy can be weird and block things like that, or who knows maybe virus scanners block "non-system" window messages.

The whole thing is dodgy so maybe I'll just put up a splash screen and make the user wait for the DllCall to finish :think:
DRocks
Posts: 559
Joined: 08 May 2018, 10:20

Re: Thread limitation

20 Oct 2018, 10:47

Lol yeah if that was the case it would not be desireable :P I will still try to mess with this and see if there are things that would help or if not just don't use that technique :)
User avatar
nnnik
Posts: 4475
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Thread limitation

21 Oct 2018, 01:50

You can do a DllCall with CreateThread.
It's fine as long as you never touch the AHK interpreter.
Recommends AHK Studio
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Thread limitation

21 Oct 2018, 09:28

nnnik wrote:
21 Oct 2018, 01:50
It's fine as long as you never touch the AHK interpreter.
What do you mean by this exactly?
Helgef
Posts: 4464
Joined: 17 Jul 2016, 01:02
Contact:

Re: Thread limitation

21 Oct 2018, 10:52

It means you shouldn't pass (to CreateThread() )a pointer from registercallback("fn"), since that is not designed to run in multiple threads. You can pass anything else which is ofc, eg a pointer to a function in some dll which is designed to run in mulitple threads.

Cheers.
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Thread limitation

21 Oct 2018, 13:19

Helgef wrote:
21 Oct 2018, 10:52
It means you shouldn't pass (to CreateThread() )a pointer from registercallback("fn"), since that is not designed to run in multiple threads. You can pass anything else which is ofc, eg a pointer to a function in some dll which is designed to run in mulitple threads.

Cheers.

Is it still safe if the function which runs in the separate OS thread receives as a parameter a pointer to my ahk var which it puts a value in, or is that still interacting with the ahk interpreter?

This is the function I'm trying to call in a separate OS thread.

Would you mind teaching me how to pass a pointer to the above function to CreateThread.

Here is my attempt so far (no idea what I'm doing).

Code: Select all

hDXVA2 := DllCall("LoadLibrary", "Str", "dxva2.dll", "Ptr")   
GetMonitorCapabilities := DllCall("GetProcAddress", "Ptr", hDXVA2 , "AStr", "GetMonitorCapabilities", "Ptr")  
DllCall("CreateThread", ptr, 0, uInt, 0, ptr, GetMonitorCapabilities, ptr, 0, uInt, 0, ptr, 0, ptr)
But then how to pass parameters to GetMonitorCapabilities?

This is how I'm doing it currently:

Code: Select all

DllCall("dxva2.dll\GetMonitorCapabilities", "Ptr", hMonitor, "UInt*", MonitorCapabilities, "UInt*", MonitorColorTemps)
Helgef
Posts: 4464
Joined: 17 Jul 2016, 01:02
Contact:

Re: Thread limitation

21 Oct 2018, 14:16

You need a go between function, which calls the relevant work function and syncs with the script when the work function finishes. You can start by reading nnnik's mcode tutorial to learn the basics. Or write a dll, there are plenty of online resources.

Cheers.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, aifritz, autoremapping, bracht, c7aesa7r, Google [Bot], mikeyww and 44 guests