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.
Extra functionality for Timeouts and Threads
Re: Extra functionality for Timeouts and Threads
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.2_05 wrote: ↑06 Oct 2020, 13:21I'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.
Interesting idea, considering the complications outlined in the docs:OnIfTimeout: Specifies a function to run automatically when an Iftimeout occurs.
This is can be a challenge when the evaluating expression cannot be interrupted without interrupting the thread.If the timeout value is exceeded, the expression continues to evaluate.
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.A_LastIfTime: The running time of the last #If [Expression].
Again, these can alternately be performed with a timer, and/or through a custom function, which might return false on a timeout.#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.
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.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.
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.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.


Re: Extra functionality for Timeouts and Threads
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.
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