Jump to content

Recommended Posts

Posted

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:

  1. sets a hotkey to Ctrl+F10 to exit the script, calling the function _Quit
  2. tells the script to call the function _ScriptStopped on exit
  3. 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:

  1. the "Script stopped" message is written to the local log file
  2. the uploadParameters function effectively uploads the parameters to a distant server
  3. the "Parameters sent" message is written to the local log file
  4. the uploadFiles function effectively uploads the log file to a distant server
  5. the script exits using the exit function
  6. the _ScriptStopped function is called
  7. the _Quit function is called
  8. the "Script stopped exited by using exit statement" message is written to the local log file
  9. the uploadParameters function does not complete
  10. no additional message is written to the log file
  11. the uploadFiles function is never called

If the _Quit method is called using F10, this is what happens:

  1. an error is triggered because the $canLog variable is not defined
  2. 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.

 

Posted

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:

#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:

  1. 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.

  2. 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."

 

Posted
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...

Posted
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)

Posted

@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?

  1. the "Script stopped" message is written to the local log file

Code hard, but don’t hard code...

Posted (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 by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
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.

 
Posted
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.

 

Posted

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 connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
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.

 
Posted

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."

Posted (edited)

I even tested it like this

 

#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 by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
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.

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...