RFC: AhkThread() usage + best practices suggestions
Posted: 20 Nov 2020, 19:55
hey there friends!
further below you will find a working code which uses AhkThread() to start 2 independent infinite loops which are only doing work if a certain variable (namely) object.error is set to none.
If either of those loops encounter a problem -> object.error is changed and both loops will stop doing work!
the main thread recognizes the error message and cleans up/fixes the issue/whatever and eventually will change object.error to none again so the 2 threads/infinite loops will do work again.
I hope this short explanation is clear enough ^^ .
-----
I would like to know if my first approach is "correct" in terms of the AhkThread() usage and maybe there is even a "better" way of doing this idea/tasks of my use case for this first approach?!
I also would love to know how to use AhkThread() with a clear separation of the FUNCTION declaration and definition?!
SPEUDOCODE example: AhkThread( _THREAD_A() , parameter_A, paramater_B, etc.... )
I tried the "ScriptIsFile" parameter but no chance it seems. Would love to see a simple example so I can move on from there if that is possible.
In any case, thanks for your time if you did read that far!
further below you will find a working code which uses AhkThread() to start 2 independent infinite loops which are only doing work if a certain variable (namely) object.error is set to none.
If either of those loops encounter a problem -> object.error is changed and both loops will stop doing work!
the main thread recognizes the error message and cleans up/fixes the issue/whatever and eventually will change object.error to none again so the 2 threads/infinite loops will do work again.
I hope this short explanation is clear enough ^^ .
-----
I would like to know if my first approach is "correct" in terms of the AhkThread() usage and maybe there is even a "better" way of doing this idea/tasks of my use case for this first approach?!
I also would love to know how to use AhkThread() with a clear separation of the FUNCTION declaration and definition?!
SPEUDOCODE example: AhkThread( _THREAD_A() , parameter_A, paramater_B, etc.... )
I tried the "ScriptIsFile" parameter but no chance it seems. Would love to see a simple example so I can move on from there if that is possible.
In any case, thanks for your time if you did read that far!
Code: Select all
#NoEnv
#Persistent
#SingleInstance FORCE
STATE := CriticalObject()
STATE.Value := "startup_clean"
STATE.error := "none"
__THREAD_A := AhkThread("
(
Sleep 2000
object:=CriticalObject(" (&STATE) ")
Loop{
if(object.error = ""none""){
Sleep 400
object.VALUE:=""do A""
if( A_Index = 20 ){
object.error := ""error from A""
}
}
Sleep 400
}
)")
__THREAD_B := AhkThread("
(
Sleep 2000
object:=CriticalObject(" (&STATE) ")
Loop{
if(object.error = ""none""){
Sleep 800
object.VALUE:=""do B""
if( A_Index = 4 ){
object.error := ""error from B""
}
}
Sleep 800
}
)")
Loop{
; MAIN THREAD here
; in case a THREAD reports an error back to "STATE.error" , error handling will be done here
; after solving the issue "STATE.error" will be set back to "none" and the THREADS will continie the INFINITE LOOP like before
ToolTip % STATE.VALUE "`n" STATE.error
Sleep 50
if(STATE.error != "none"){
STATE.Value := "fixing the issue"
}
}
Esc::ExitApp
F1::STATE.error := "error"
F2::STATE.error := "none"