Loceka Posted October 16, 2020 Posted October 16, 2020 Hello, I have 2 issues with the following code, using AutoIt v3.3.14.5: HotKeySet("^{F10}", "_Quit") OnAutoItExitRegister("_ScriptStopped") if Random(0, 1, 1) then _Quit(false) func _Quit($canLog = true, $exitReason = "") ; the function writeLog writes to a local file if $canLog then writeLog("Script stopped" & ($exitReason ? ", exited by " & $exitReason : "")) ; the function uploadParameters performs an HTTP GET request using _INetGetSource("my_url") uploadParameters(true) writeLog("Parameters sent") ; the function uploadFiles performs an HTTP POST request and uploads the Log file uploadFiles($LOG) if not $exitReason then exit endFunc func _ScriptStopped() switch @exitMethod case $EXITCLOSE_BYEXIT _Quit(true, "using exit statement") endSwitch endFunc First let's describe what the main script does: sets a hotkey to Ctrl+F10 to exit the script, calling the function _Quit tells the script to call the function _ScriptStopped on exit half of the time, exits the script "naturally" by calling the _Quit method If the _Quit method is called by the main script (not using F10), this is what happens: the "Script stopped" message is written to the local log file the uploadParameters function effectively uploads the parameters to a distant server the "Parameters sent" message is written to the local log file the uploadFiles function effectively uploads the log file to a distant server the script exits using the exit function the _ScriptStopped function is called the _Quit function is called the "Script stopped exited by using exit statement" message is written to the local log file the uploadParameters function does not complete no additional message is written to the log file the uploadFiles function is never called If the _Quit method is called using F10, this is what happens: an error is triggered because the $canLog variable is not defined the _ScriptStopped function is called and does the same as above So I have 2 issues here... First when a function is called using HotKeySet(), the function's default parameters are not considered at all. A workaround for this is to wrap the function with parameters in another and call the other one in HotKeySet() (which is what I actually do) but I am very curious to understand why it behaves like that. It pretty much defies any logic. Then, and this is much more problematic, when a script is stopped using OnAutoItExitRegister(), it doesn't perform HTTP calls (and maybe other things too). I can see no workaround for this one and it is kind of a problem to me. Why does it behaves like this and is there anything to do? Thanks, Loceka.
Loceka Posted October 16, 2020 Author Posted October 16, 2020 EDIT : on step "9. the uploadParameters function does not complete ", in fact the function is never called
Developers Jos Posted October 16, 2020 Developers Posted October 16, 2020 The posted script can't be the whole thing ...right? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Loceka Posted October 16, 2020 Author Posted October 16, 2020 No, it aint the whole script, it would be a pretty useless script if it was, and it would lack at least 3 functions and a global variable. But I described those functions (and global var). Still, here is a standalone test: expandcollapse popup#include <AutoItConstants.au3> #include <Date.au3> #include <FileConstants.au3> #include <Inet.au3> #include <Json.au3> ;https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn #include <WinAPIFiles.au3> HotKeySet("^{F10}", "_Quit") ; Ctrl+F10 to quit OnAutoItExitRegister("_ScriptStopped") writeLog("script started") sleep(5*1000) ; sleep 5 seconds _Quit(false) func _ScriptStopped() switch @exitMethod case $EXITCLOSE_BYEXIT _Quit(true, "using exit statement") endSwitch endFunc func _Quit($canLog = true, $exitReason = "") if $canLog then writeLog("Script stopped" & ($exitReason ? ", exited by " & $exitReason : "")) for $i = 1 to 2 step 1 otherFunction($i) next if not $exitReason then exit endFunc func otherFunction($call) writeLog("otherFunction "&$call&" entered") local $checkURL = "https://google.com/" local $json = Json_ObjCreate() Json_ObjPut($json, "stopped", true) _INetGetSource($checkURL & (StringInStr($checkURL, "?") ? "&" : "?") & "params=" & Json_Encode($json)) writeLog("otherFunction "&$call&" exited") endFunc func writeLog($text) local $logFile = FileOpen("textExit.log", $FO_APPEND) FileWriteLine($logFile, _NowCalc() & ": " & $text) FileClose($logFile) endFunc and here is the resulting logs and behaviour: Standard execution 2020/10/16 10:40:06: script started 2020/10/16 10:40:11: otherFunction 1 entered 2020/10/16 10:40:12: otherFunction 1 exited 2020/10/16 10:40:12: otherFunction 2 entered 2020/10/16 10:40:12: otherFunction 2 exited 2020/10/16 10:40:12: Script stopped, exited by using exit statement 2020/10/16 10:40:12: otherFunction 1 entered note that the function does not complete when called using OnAutoItExitRegister. On my tests it seems it only happens when a Json object is created. Using Ctrl+F10 2020/10/16 10:41:01: script started An error popup is triggered with the message "Error: Variable used without being declared."
JockoDundee Posted October 16, 2020 Posted October 16, 2020 1 hour ago, Loceka said: Why does it behaves like this and is there anything to do? It would appear that you are recursively calling _Quit, no? _Quit(false)—> exit—> _ScriptStopped()—>_Quit(true,”using”)—> exit—> Maybe throw in an OnAutoITUnRegister(“_ScriptStopped”) just so it doesn’t loop. also and btw, you can call exit with a exit code and pick it up in the exit function if that helps any. Code hard, but don’t hard code...
Loceka Posted October 16, 2020 Author Posted October 16, 2020 8 minutes ago, JockoDundee said: It would appear that you are recursively calling _Quit, no? _Quit(false)—> exit—> _ScriptStopped()—>_Quit(true,”using”)—> exit—> No, I do a test on the $exitReason to avoid calling exit when the caller is _ScriptStopped. And I do that because I have an error on @exitMethod if I call _Quit normally (and I don't know how to either test its existence or catch it)
Developers Jos Posted October 16, 2020 Developers Posted October 16, 2020 My point was that it helps when you post something runable so we can test. SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
JockoDundee Posted October 16, 2020 Posted October 16, 2020 @Loceka ok, but how is it even possible that the _Quit(false) call is causing the log to be written to, since $canlog will be set to false? the "Script stopped" message is written to the local log file Code hard, but don’t hard code...
Loceka Posted October 16, 2020 Author Posted October 16, 2020 yup, disregard the first step ^^ My third post provides a "working" example though
bogQ Posted October 16, 2020 Posted October 16, 2020 (edited) Crash error: Your exit is triggered 2 times on exit with HotKeySet first time and second time with OnAutoItExitRegister. Try usin OnAutoItExitUnRegister as first line in _Quit finc Variable undeclared: $canLog is not declared on _Quit HotKeySet, i do not think HotKeySet can trigger defoult parrametars from funcs. (i am maybe wrong on this) WorkAround something like: func _Quit($canLog = true, $exitReason = "") OnAutoItExitUnRegister("_ScriptStopped") if IsDeclared("canLog") And $canLog then writeLog("Script stopped" & ($exitReason ? ", exited by " & $exitReason : "")) for $i = 1 to 2 step 1 otherFunction($i) next if (not IsDeclared("exitReason")) then exit endFunc Adapt it to your needs. Edited October 16, 2020 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.
Loceka Posted October 16, 2020 Author Posted October 16, 2020 49 minutes ago, bogQ said: Crash error: Your exit is triggered 2 times on exit with HotKeySet first time and second time with OnAutoItExitRegister. Try usin OnAutoItExitUnRegister as first line in _Quit finc my problem with the OnAutoItExitUnRegister() is not the fact it is called twice, it is about why it does not do everything in the function.
bogQ Posted October 16, 2020 Posted October 16, 2020 Well looking @ log i do get Quote Using hotkey i got 2020/10/16 16:10:31: script started 2020/10/16 16:10:33: otherFunction 1 entered 2020/10/16 16:10:33: otherFunction 1 exited 2020/10/16 16:10:33: otherFunction 2 entered 2020/10/16 16:10:33: otherFunction 2 exited waiting 5 seconds to close i got 2020/10/16 16:10:41: script started 2020/10/16 16:10:46: otherFunction 1 entered 2020/10/16 16:10:47: otherFunction 1 exited 2020/10/16 16:10:47: otherFunction 2 entered 2020/10/16 16:10:47: otherFunction 2 exited So using function that i posted what part of script its not executing? TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.
pixelsearch Posted October 16, 2020 Posted October 16, 2020 Hi Loceka, Concerning the fatal error : "Variable used without being declared" if $canLog then writeLog("Script stopped" & ($exitReason ? ", exited by " & $exitReason : "")) if ^ ERROR The help file (topic HotKeySet) clearly stipulates this : "The called function can not be given parameters. They will be ignored" So why not get rid of this issue first, before anything else, by creating 2 Global variables at the beginning of the script : Global $canLog = true, $exitReason = "" * Then, if the _Quit() function is called by Ctrl+F10, both variables will already have their correct values assigned. * If the program calls the _Quit function (twice in your example), you'll have to redefine both variables just before the call. At least it would give you a peace of mind... before solving the other issues "I think you are searching a bug where there is no bug... don't listen to bad advice."
bogQ Posted October 16, 2020 Posted October 16, 2020 (edited) I even tested it like this expandcollapse popup#include <AutoItConstants.au3> #include <Date.au3> #include <FileConstants.au3> #include <Inet.au3> #include <Json.au3>;https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn #include <WinAPIFiles.au3> Global $log, $statement HotKeySet("^{F10}", "_QuitHotkey") ; Ctrl+F10 to quit OnAutoItExitRegister("_QuitMain") AutoItSetOption('TrayAutoPause', 0) ;~ //EXITCLOSE_NORMAL but with right click on icon exit writeLog("script started") Sleep(5 * 1000) ; sleep 5 seconds _LogFunc() ;~ //EXITCLOSE_NORMAL but only if you did not set variable with _LogFunc() Func _QuitMain() If ($statement == Null Or Not IsDeclared("statement") Or $statement == "") Then Switch @exitMethod Case $EXITCLOSE_NORMAL $log = True $statement = "exit using $EXITCLOSE_NORMAL" Case $EXITCLOSE_BYEXIT $log = True $statement = "exit using $EXITCLOSE_BYEXIT" Case $EXITCLOSE_BYCLICK $log = True $statement = "exit using $EXITCLOSE_BYCLICK" Case $EXITCLOSE_BYLOGOFF $log = True $statement = "exit using $EXITCLOSE_BYLOGOFF" Case $EXITCLOSE_BYSHUTDOWN $log = True $statement = "exit using $EXITCLOSE_BYSHUTDOWN" EndSwitch EndIf writeLog($statement) ConsoleWrite("test " & @CRLF) For $i = 1 To 2 Step 1 otherFunction($i) Next EndFunc ;==>_QuitMain Func _QuitHotkey() $log = True $statement = "exit using hotkey" Exit EndFunc ;==>_QuitHotkey Func _LogFunc() $log = True $statement = "using some function to log file" EndFunc ;==>_LogFunc Func otherFunction($call) writeLog("otherFunction " & $call & " entered") Local $checkURL = "https://google.com/" Local $json = Json_ObjCreate() Json_ObjPut($json, "stopped", True) _INetGetSource($checkURL & (StringInStr($checkURL, "?") ? "&" : "?") & "params=" & Json_Encode($json)) writeLog("otherFunction " & $call & " exited") EndFunc ;==>otherFunction Func writeLog($text) Local $logFile = FileOpen("textExit.log", $FO_APPEND) FileWriteLine($logFile, _NowCalc() & ": " & $text) FileClose($logFile) EndFunc ;==>writeLog no need to call functions all over the place, OnAutoItExitRegister will handle exit command so you just need to call Exit or let the script to exit itself Edit: and log file output Quote 2020/10/16 16:57:06: script started 2020/10/16 16:57:11: using some function to log file 2020/10/16 16:57:11: otherFunction 1 entered 2020/10/16 16:57:12: otherFunction 1 exited 2020/10/16 16:57:12: otherFunction 2 entered 2020/10/16 16:57:12: otherFunction 2 exited 2020/10/16 16:57:19: script started 2020/10/16 16:57:21: exit using $EXITCLOSE_BYCLICK 2020/10/16 16:57:21: otherFunction 1 entered 2020/10/16 16:57:21: otherFunction 1 exited 2020/10/16 16:57:21: otherFunction 2 entered 2020/10/16 16:57:22: otherFunction 2 exited 2020/10/16 16:57:23: script started 2020/10/16 16:57:24: exit using hotkey 2020/10/16 16:57:24: otherFunction 1 entered 2020/10/16 16:57:25: otherFunction 1 exited 2020/10/16 16:57:25: otherFunction 2 entered 2020/10/16 16:57:25: otherFunction 2 exited edit wrong macro on switch Edited October 16, 2020 by bogQ TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now