Extra functionality for Timeouts and Threads

Propose new features and changes
2_05
Posts: 21
Joined: 09 May 2018, 10:28

Extra functionality for Timeouts and Threads

Post by 2_05 » 06 Oct 2020, 13:21

I've been having some issues with my script not responding to hotkeys anymore. It seems like the system stops calling the script's keyboard hook.
Having some Callbacks that are often fired and dynamic hotkey assignments that could prevent execution of #If Execution it is not evident where I need to make changes.
I'd like to better understand where in my script stalling occurs and under what circumstances. Therefore I need to know when my script is stalled. I tried it with A_TickCount, but this does not show how long execution is delayed before the #If [Expression] can run.
I also like to better understand how often script execution is interrupted and how many threads are active.

I believe the following functions may help provide insight and could be useful for more people.
OnIfTimeout: Specifies a function to run automatically when an Iftimeout occurs.
A_LastIfTime: The running time of the last #If [Expression].
#MessageTimeout: Sets the maximum time that may be spent evaluating a received Message.
OnMessageTimeout: Specifies a function to run automatically when an MessageTimeout occurs.
A_LastMessageTime: The running time of the evaluation of the last Message.
#CallbackTimeout: Sets the maximum time that may be spent evaluating a Callback.
OnCallbackTimeout: Specifies a function to run automatically when an CallbackTimeout occurs.
A_LastCallbackTime: The running time of the last Callback.

A_Threads: The number of unfinished threads.
A_ThreadTime: The number of milliseconds that have elapsed since the current thread was launched.
A_ThreadInterrupts: The number of times the current thread was interrupted.
A_ThreadId: A unique ID associated with the current thread. Allows for generating unique variable names per thread.
ThreadGlobal/ThreadLocal: A variable declaration that is unique for the current thread. It is automatically destroyed when the thread finishes. Ensures the variable is not changed when the same function is called by an interrupting thread.

User avatar
lmstearn
Posts: 375
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn
Contact:

Re: Extra functionality for Timeouts and Threads

Post by lmstearn » 29 Oct 2020, 08:51

2_05 wrote:
06 Oct 2020, 13:21
I've been having some issues with my script not responding to hotkeys anymore. It seems like the system stops calling the script's keyboard hook.
Having some Callbacks that are often fired and dynamic hotkey assignments that could prevent execution of #If Execution it is not evident where I need to make changes.
I'd like to better understand where in my script stalling occurs and under what circumstances. Therefore I need to know when my script is stalled. I tried it with A_TickCount, but this does not show how long execution is delayed before the #If [Expression] can run.
I also like to better understand how often script execution is interrupted and how many threads are active.

I believe the following functions may help provide insight and could be useful for more people.
Hi @2_05, issues with script hotkey response aren't new, have you researched into how they are handled by some of AHK's more experienced scripters? In many cases TimeSinceThisHotkey can be used to to assist time calculations. There is also KeyHistory, and to test a problem script, reduce the number of hotkeys and complex expressions, and apply it to something like Yunit.
OnIfTimeout: Specifies a function to run automatically when an Iftimeout occurs.
Interesting idea, considering the complications outlined in the docs:
If the timeout value is exceeded, the expression continues to evaluate.
This is can be a challenge when the evaluating expression cannot be interrupted without interrupting the thread.
A_LastIfTime: The running time of the last #If [Expression].
The last #If? Being directives, the best bet in a script is to implement a function after each #If and calculate the time spent to process that, else, as with the items below, AHK has to vectorize all possible Callbacks and Messages and then set up the wait loops, which would slow it considerably.
#MessageTimeout: Sets the maximum time that may be spent evaluating a received Message.
OnMessageTimeout: Specifies a function to run automatically when an MessageTimeout occurs.
A_LastMessageTime: The running time of the evaluation of the last Message.
#CallbackTimeout: Sets the maximum time that may be spent evaluating a Callback.
OnCallbackTimeout: Specifies a function to run automatically when an CallbackTimeout occurs.
A_LastCallbackTime: The running time of the last Callback.
Again, these can alternately be performed with a timer, and/or through a custom function, which might return false on a timeout.
A_Threads: The number of unfinished threads.
A_ThreadTime: The number of milliseconds that have elapsed since the current thread was launched.
A_ThreadInterrupts: The number of times the current thread was interrupted.
A_ThreadId: A unique ID associated with the current thread. Allows for generating unique variable names per thread.
These also entail the construction of massive XRef tables of the script hotkeys, SetTimer subroutines, custom menu items, and GUI events. The impact of such on a fast system might be minimal, though.
ThreadGlobal/ThreadLocal: A variable declaration that is unique for the current thread. It is automatically destroyed when the thread finishes. Ensures the variable is not changed when the same function is called by an interrupting thread.
This really is the same effect as implementing all the code of the thread into a function. There's heaps of of other things to consider as well, hopefully, for others to pitch in. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

User avatar
nnnik
Posts: 4480
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Extra functionality for Timeouts and Threads

Post by nnnik » 20 Nov 2020, 04:49

Dont use #if unless your program structure allows it.

To elaborate - the execution of #if may get delayed while another piece of code is running.
AutoHotkey itself eventually triggers an IfTimeOut.
However the OS itself might already disconnect AutoHotkey from the message queue when that happens.

This leads me to the following conclusions.
You cannot use #if if you have long sequences of execution that could delay the #if in your script.
You shouldnt do extensive evaluation of a long condition on your #if but rather evaluate in the normal script and store that value and only respond with that value on #if.
Recommends AHK Studio

Post Reply

Return to “Wish List”