OnExit not triggered when script contains #InstallKeybdHook

Get help with using AutoHotkey and its commands and hotkeys
joefiesta
Posts: 131
Joined: 24 Jan 2016, 13:54

OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 13:44

In the following, the OnExit routine is not triggered by the EXIT command:

Code: [Select all] [Download] GeSHi © Codebox Plus

#InstallKeybdHook
OnExit, ExitRoutine
exit
ExitRoutine:
msgbox exiting test....
exitapp


However, remove the #InstallKeybdHook directive and it works just fine. I can find no documentation about #InstallKeybdHook or OnExit being related.

thank you

Joe Petree
TheDewd
Posts: 874
Joined: 19 Dec 2013, 11:16
Location: USA

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 14:28

You are not calling the ExitApp command outside of the ExitRoutine label.

This directive (#InstallKeybdHook) also makes a script persistent, meaning that ExitApp should be used to terminate it.
User avatar
derz00
Posts: 478
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 18:46

I think people should be slower to post things in Bug Reports. But maybe the moderators don't mind.
try it and see
...
joefiesta
Posts: 131
Joined: 24 Jan 2016, 13:54

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Aug 2017, 20:31

I spent 2 hours working on this before I discovered the extremely subtle relationship between EXIT, ONEXIT, a persistent script and #InstallkeybdHook. I read ALL the documentation about each. thoroughly. The DEWD did point me in the right direction to understanding the issue. (However, I DID READ that quote and believed that my code should have made my Exitapp execute.)

Yes, this is not a bug. and should be moved out of the bug forum. That is not to say, however, that I should not have posted the issue. I must also add that I searched both the old and new AHK forums for help on this issue. I found NOTHING even similar to my problem. Having used AHK for about 7 years now, I usually find that my difficulty in understanding comes from the difficulty of wading through the documentation. I feel this is entirely about the idiosyncratic way EXIT works in this context, and just wish there had been one small note about that.

Thank you for you input DEWD. As to the other respondent, I think you were just wasting your time being condescending.
User avatar
derz00
Posts: 478
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: OnExit not triggered when script contains #InstallKeybdHook

04 Aug 2017, 12:21

I apologize for my condescending attitude. And now I also realize that I was wasting my time posting that. I should have thought once or twice more before I hit submit. Now I must take more time to undo the damage done. I do find it amusing/annoying the way so many people post things in bug reports when they have no idea how it is actually supposed to work. But reading your second post, I see you did your homework, and the documentation itself can receive the blame. I guess my own attitude of being smart about AHK (which I undoubtedly am not) is to blame for that post.

Thanks for not being too polite to correct me!
try it and see
...
lexikos
Posts: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: OnExit not triggered when script contains #InstallKeybdHook

27 Dec 2017, 20:52

I have been going through old bug reports, and have moved this topic to Ask for Help.
I found NOTHING even similar to my problem.
Perhaps it's not a common mistake.
I feel this is entirely about the idiosyncratic way EXIT works in this context, and just wish there had been one small note about that.
Exactly what would you add to the documentation?

I would have thought that the presence of a tray icon would give away the fact that the script had not exited. It would be even more obvious if the script was single-instance, since a second attempt to run the script would tell you it is already running. If it is not exiting, there is no reason for OnExit to be called.

It seems that you assumed Exit would cause the entire script to exit; but why? I think the difference between Exit and ExitApp is already quite clearly documented. I don't understand why anyone would use Exit if the intention is to unconditionally exit the script.
ExitApp wrote:Terminates the script unconditionally.

Exit wrote:Exits the current thread or (if the script is not persistent and contains no hotkeys) the entire script.

Remarks

If the script has no hotkeys, isn't persistent, and hasn't requested the Num/Scroll/CapsLock key(s) to be kept AlwaysOn or AlwaysOff, it will terminate immediately when Exit is encountered (except if it has an OnExit subroutine).

Otherwise, the Exit command terminates the current thread. In other words, the stack of subroutines called directly or indirectly by a menu, timer, or hotkey subroutine will all be returned from as though a Return were immediately encountered in each. If used directly inside such a subroutine -- rather than in one of the subroutines called indirectly by it -- Exit is equivalent to Return.

Use ExitApp to completely terminate a script that is persistent or contains hotkeys.
joefiesta
Posts: 131
Joined: 24 Jan 2016, 13:54

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Apr 2018, 13:52

I still don't understand. I fully understand that EXIT may not exit a script. I also read that EXITAPP should ALWAYS exit a script.

I do not expect the EXIT command in my script to EXIT the script. What I do expect to see happen--but I don't--is this:

1. #InstallKeybdHook is processed. The sample script is, of course, extracted from a much larger one, which requires the KeyboardHook. I understand this makes the script PERSISTENT.
2. OnExit, exitroutine. I expect this command to register the EXITROUTINE subroutine when the script processes an exit.
3. EXIT. I expect this command to do only one thing: Invoke the registered EXITROUTINE subroutine.

Number three does not occur. Why? Neither the MSGBOX command is processed, nor the EXITAPP command.

The behavior is almost AS IF the EXIT command were just being ignored (presumably because the script is Persistent).
lexikos
Posts: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: OnExit not triggered when script contains #InstallKeybdHook

03 Apr 2018, 22:05

If you do not expect the Exit command to exit your script, you should not expect it to trigger the OnExit callback.
Specifies a subroutine or function to run automatically when the script exits.
joefiesta
Posts: 131
Joined: 24 Jan 2016, 13:54

Re: OnExit not triggered when script contains #InstallKeybdHook

04 Apr 2018, 09:22

@Lexikos: thank you. I FINALLY GET IT. I overlooked (at EXIT)
(if the script is not persistent and contains no hotkeys)


Exit does NOTHING sometimes. This is VERY UNINTUITIVE. Gee, I could use it like a NOOP sometimes (something I often wondered why it doesn't exist for writing IF..ELSE statements). (Kinda just joking.)

One question then: Is the auto-execute section of a script considered a THREAD?

I ask this for two reasons.

(1) I find the definition of THREAD somewhat lacking, in that it says
The current thread is defined as the flow of execution invoked by the most recent event; examples include hotkeys, SetTimer subroutines, custom menu items, and GUI events
By lacking, I mean "examples include" does not limit threads to other events.

(2) I run a script with only an auto-execute section, for example

#persistent
exit

and Windows task manager shows the script has having ONE thread. I interpret your definition of thread as HAVING TO BE AN EVENT, but what event is the above? (Or, to put it another way, it seems the mere execution of the script creates a THREAD--something I would definitively NOT question.)
lexikos
Posts: 5897
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: OnExit not triggered when script contains #InstallKeybdHook

04 Apr 2018, 17:21

Exit does NOTHING sometimes.
No. It always exits the thread. This is its primary purpose, hence the first four words of its documentation being "Exits the current thread". Exiting the script is a side-effect; if the script has nothing left to do, there's no reason to leave the process running.
One question then: Is the auto-execute section of a script considered a THREAD?
No, the auto-execute section is a subroutine, but it is called in its own thread. Without a thread, there is no "flow of execution".
... and Windows task manager shows the script has having ONE thread.
Although AutoHotkey doesn't actually use multiple threads, it simulates some of that behavior
Source: Threads

... but what event is the above?
The script starting.

Return to “Ask For Help”

Who is online

Users browsing this forum: burque505, eduardobedoya, hopit, leuce, smarq8 and 28 guests