Jump to content

Child script (created by parent script) can't use @exitMethod


Recommended Posts

Thanks to the help of many experts here, I've written and compiled a script that runs in the background. When it launches, it writes (to the %temp% folder) a "child" script with a temporary filename, and than launches that "child" script, which creates a tray icon that can be used without interrupting processing in the "parent" script.

The "child" script includes these lines, which close the "parent" script when the user clicks "Exit" on the child's icon:

OnAutoItExitRegister (_Exit)
...
Func _Exit()
    RunWait('TaskKill /PID ' & $CmdLine[1] & ' /F', '', @SW_HIDE)
EndFunc

The parent script includes these lines so that the parent won't display an icon and so the child can be run from a compiled version of the script:

#NoTrayIcon
#pragma compile(AutoItExecuteAllowed, True)

This all works perfectly, except for one thing. When I shut down or restart Windows with the script running in the background, the TaskKill command gets in the say of the shutdown. So I tried adding these lines to the child script:

Func _Exit()
Switch @exitMethod
    Case 0, 1, 2
        RunWait('TaskKill /PID ' & $CmdLine[1] & ' /F', '', @SW_HIDE)
        FileDelete (@ScriptFullPath)
    Case 3, 4
EndSwitch
EndFunc

But when I run the script, and the child launches, AutoIt generates an error message pointing to "@exitMethod".

Is there some reason why @exitMethod is invalid in a script launched from a parent? Is there something I should add to that child script?

I haven't found any information on this, and will be grateful for any help.

Edited by emendelson
Link to comment
Share on other sites

I've attached a screenshot of the error message. If the image is too small, this is what it says:

Line 59 (File "C:\Users\Edward\AppData\Local\Temp\~EE86.tmp"):

Switch @exitMethod
Switch ^ ERROR

Error: Unknown macro.

I tried (as you suggested) setting $myVar = @exitMethod at the top of the script (NOT inside the _Exit function), but that produced the same error: @exitMethod was an "Unknown macro."

If I take my complete "child" script and run it from the script editor, it exits normally, and the @exitMethod macro works correctly. (I modified it to write the exitMethod to the console in order to test it.) The "Unknown macro" error only occurs when the script is run as a "child" from my larger compiled script.

As far as I can tell, no include is needed for @exitMethod. The following script works perfectly in the script editor, and this is the complete script, which has no #include lines:

OnAutoItExitRegister(_Exit)
Exit
Func _Exit()
    Switch @exitMethod
        Case 0, 1, 2
            ConsoleWrite("case 1, 2, or 3" & @LF)
        Case 3, 4
            ConsoleWrite("case 3 or 4" & @LF)
    EndSwitch
    Exit
EndFunc   ;==>_Exit

I doubt there's a bug here, after all these years of development, but I wonder what else needs to be done to make @exitMethod work in a script that is started from a compiled script that has "#pragma compile(AutoItExecuteAllowed, True)".

Error.png

EDIT: This may be a related issue, except that my script doesn't call the _Exit function directly (EDIT: I didn't notice that my script DID in fact call the _Exit function directly... Solved for me by Danp2!)

There doesn't seem to be a workaround, or if there is, I can't figure it out from that thread.

Edited by emendelson
Link to comment
Share on other sites

@Danp2 - YES! You caught the problem! Thank you!

I had included this item on the tray menu:

Func ExitScript()
    TrayItemSetState($exitItem, 4)
    $e = MsgBox(4356, $t, 'Do you want to stop watching the folder ' & $watchPath & '?' & @CRLF & @CRLF & _
            'If you choose Yes, the automatic printing feature will stop working until you restart me.')
    If $e = 6 Then _Exit()
EndFunc   ;==>ExitScript

When I saw your message, I changed the second to last line to:

If $e = 6 Then Exit

And now everything works correctly. Thank you again!

Edited by emendelson
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...