Thread global variables and thread ID
Posted: 16 Sep 2018, 04:51
Many applications can be open multiple times, so there are several PID's active for the same application. If a Thread is started on one process of an application, I want to insure that all functions work on the same processes even when other windows have become active in between. I know a few ways of doing this.
Similar to the definition of super global variables global var1, I would propose thread variables to be defined by thread var1 or threadglobal var1.
Besides the thread variables it is also helpful to have a unique ID for each thread within the script, e.g. A_ThreadID. With A_ThreadID you can for example store values in a global associative array and call global PIDs[A_ThreadID] to retrieve thread specific values. I'm not sure if Autohotkey uses thread_id's internally, I think in that case it's a small step to make this value available to the script.
- Pass the PID along with each function call.
- Set the LastFoundWindow. This required all functions to always restore the 'last found window' if that function temporarily has to act on another window. The last found window is thread specific.
- Store the PID in a global variable. This is not stable when a thread can be interrupted by another thread which changes the global variable.
Similar to the definition of super global variables global var1, I would propose thread variables to be defined by thread var1 or threadglobal var1.
Besides the thread variables it is also helpful to have a unique ID for each thread within the script, e.g. A_ThreadID. With A_ThreadID you can for example store values in a global associative array and call global PIDs[A_ThreadID] to retrieve thread specific values. I'm not sure if Autohotkey uses thread_id's internally, I think in that case it's a small step to make this value available to the script.