OnExit reason is always "0" (v2.0.15)

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
smrt1
Posts: 36
Joined: 27 Mar 2022, 04:47

OnExit reason is always "0" (v2.0.15)

Post by smrt1 » 19 May 2024, 03:53

[Moderator's note: Topic moved from Bug Reports.]

Hello,

Because I couldn't retrieve the exit reason, I've wrote this little exit function which reveal some kind of bug in "OnExit".
Whatever method used to terminate the script (reload, system shutdown, etc.) it always produce a single value: 0

Code: Select all

OnExit(DoExit)
DoExit(o*) {
	for k, v in o
		fileappend(k "=" v "`n", "why.txt")
}
Last edited by lexikos on 23 May 2024, 23:51, edited 2 times in total.
Reason: Moved

User avatar
Ragnar
Posts: 630
Joined: 30 Sep 2013, 15:25

Re: OnExist reason is always "0" (v2.0.15)

Post by Ragnar » 19 May 2024, 05:43

When I run this script, I get the following output:

Code: Select all

1=Exit
2=0
As expected, the exit reason is "Exit" and the exit code is 0. The exit code is always 0 unless specified otherwise via Exit or ExitApp.

just me
Posts: 9579
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: OnExist reason is always "0" (v2.0.15)

Post by just me » 19 May 2024, 07:10

Code: Select all

#Requires AutoHotkey v2.0
OnExit(DoExit)
DoExit(o*) {
	for k, v in o
		MsgBox(v, k)
}
ExitApp 999

User avatar
smrt1
Posts: 36
Joined: 27 Mar 2022, 04:47

Re: OnExist reason is always "0" (v2.0.15)

Post by smrt1 » 19 May 2024, 08:42

Ragnar wrote:
19 May 2024, 05:43
When I run this script, I get the following output:

Code: Select all

1=Exit
2=0
As expected, the exit reason is "Exit" and the exit code is 0. The exit code is always 0 unless specified otherwise via Exit or ExitApp.
I have several OnExit(f) in my script - maybe it has something to do - and when I call ExitApp(999) I get this, nothing else:

Code: Select all

1=999
.

User avatar
Ragnar
Posts: 630
Joined: 30 Sep 2013, 15:25

Re: OnExist reason is always "0" (v2.0.15)

Post by Ragnar » 19 May 2024, 09:10

Then show us your script. If it is too large or contains sensitive data, reduce it to a minimum where this supposed bug still occurs.

User avatar
smrt1
Posts: 36
Joined: 27 Mar 2022, 04:47

Re: OnExist reason is always "0" (v2.0.15)

Post by smrt1 » 19 May 2024, 13:16

Ragnar wrote:
19 May 2024, 09:10
Then show us your script. If it is too large or contains sensitive data, reduce it to a minimum where this supposed bug still occurs.
Same behavior with this:

Code: Select all

class runbox {

	static __New() {
		OnExit(runbox.Exit)
	}

	static Exit(*) {
	}
}


class runbox2 {

	static __New() {
		OnExit(runbox2.Exit)
	}

	static Exit(o*) {
		for k, v in o
				fileappend(k "=" v "`n", "why.txt")
	}
}




Loop
	Sleep 1000

User avatar
Ragnar
Posts: 630
Joined: 30 Sep 2013, 15:25

Re: OnExist reason is always "0" (v2.0.15)

Post by Ragnar » 19 May 2024, 14:40

Thanks, but it's not a bug. The exit reason is stored in the hidden first parameter named this. See https://www.autohotkey.com/docs/v2/Objects.htm#Custom_Classes_method

User avatar
smrt1
Posts: 36
Joined: 27 Mar 2022, 04:47

Re: OnExist reason is always "0" (v2.0.15)

Post by smrt1 » 19 May 2024, 16:35

Ragnar wrote:
19 May 2024, 14:40
Thanks, but it's not a bug. The exit reason is stored in the hidden first parameter named this. See https://www.autohotkey.com/docs/v2/Objects.htm#Custom_Classes_method
Ok, thank you.
I'll never understand this "this" thing :(

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: OnExist reason is always "0" (v2.0.15)

Post by lexikos » 23 May 2024, 23:50

smrt1 wrote:
19 May 2024, 16:35
I'll never understand this "this" thing :(
It's just a parameter, not difficult to understand. These are basically equivalent:

Code: Select all

class runbox {
	static Exit(o*) {
	}
}

Code: Select all

runbox.DefineProp("Exit", {call: runbox_Exit})
class runbox {
}
runbox_Exit(this, o*) {
}
... and not much different to:

Code: Select all

runbox := {Exit: runbox_Exit}
runbox_Exit(this, o*) {
}
In all of these cases, runbox.Exit is just a reference to a function which has a parameter named this, and puts all of the remaining parameters in o.

User avatar
smrt1
Posts: 36
Joined: 27 Mar 2022, 04:47

Re: OnExit reason is always "0" (v2.0.15)

Post by smrt1 » 24 May 2024, 13:05

@lexikos Thank you.

Post Reply

Return to “Ask for Help (v2)”