Execution continues after Reload command

Propose new features and changes
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Execution continues after Reload command

28 Nov 2019, 02:27

edit: to moderators, I should have posted this in the "Suggestions on documentation improvements" - sorry.

Code: Select all

MsgBox About to reload
Reload
FileAppend , After reload , Debug.txt
return

The documentation should mention that execution will continue after the Reload command for an unpredictable amount of time, which appears to be the time until the new instance of the script happens to shut down the current instance.

A separate issue is that if the Reload fails, then we need to put some code afterwards to handle it. An obvious choice is to ExitApp, but we can't do that because it triggers the OnExit routine, which could be interrupted by the new instance forcing shutdown of the current instance. We could sleep for a few seconds to give it some time, but even that doesn't guarantee it won't be interrupted, as the new instance might be delayed for a few seconds due to busy system or antivirus waiting for user prompt to allow the new instance. So all we can do after Reload is Exit or Return, which leaves the script running if Reload failed, which may be undesirable.
Last edited by pneumatic on 28 Nov 2019, 17:53, edited 1 time in total.
User avatar
Delta Pythagorean
Posts: 567
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: Execution continues after Reload command

28 Nov 2019, 14:19

A. You can use a quick workaround of using the Run command to reload the script, just remember that SingleInstance will screw you over.
B. You can force exiting the script by using Process, Close, % PID.
C. You can use Thread and Interrupt to create forced threads.

- [AHK].......: 1.1.33.02 Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Execution continues after Reload command

28 Nov 2019, 14:41

@pneumatic: Interesting question.
Possibly, as a workaround, you could write some custom code at the top of script, in place of #SingleInstance force.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Execution continues after Reload command

28 Nov 2019, 17:47

This is the workaround I'm currently using.

Code: Select all

ExitRoutine("But don't ExitApp at the end")
Reload
ExitRoutine("Don't do routine, just go straight to ExitApp")
The documentation is not to blame for this, as it does state that Reload will interrupt the OnExit routine.

My suggestion was only to inform the user that execution will continue after Reload.

Also I've just realised I should have posted this in the "Suggestions on documentation improvements" thread, so if a moderator could move it there it would be appreciated.
gregster
Posts: 4910
Joined: 30 Sep 2013, 06:48

Re: Execution continues after Reload command

28 Nov 2019, 21:18

Your topic could be moved to the Wishlist subforum, where also "Suggestions on documentation improvements" is located.

But I don't think it could be fused with the Suggestions topic in a consistent manner - but you could post a link to this topic there, if you think it's necessary.

Edit: I moved it now (subject to a better idea).
joefiesta
Posts: 380
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Execution continues after Reload command

29 Nov 2019, 09:52

To make sure no code is executed after the RELOAD command:

just add EXITAPP statement next

Code: Select all

Reload
Exitapp
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Execution continues after Reload command

29 Nov 2019, 17:03

joefiesta wrote:
29 Nov 2019, 09:52
To make sure no code is executed after the RELOAD command:
just add EXITAPP statement next

As mentioned, ExitApp jumps to OnExit routine, which can be interrupted by the new script instance spawned by Reload.

This is what was happening to my script; I have some DllCalls in my OnExit routine which were getting interrupted and wreaking havoc.
Last edited by pneumatic on 29 Nov 2019, 17:24, edited 1 time in total.
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Execution continues after Reload command

29 Nov 2019, 17:14

On an i7-4790k I get ~200 FileAppends after Reload. A_TickCount doesn't change, so it's < 15.6ms or whatever the system time slice is.

Code: Select all

#Persistent
OnExit , Exit
SetTimer , Reload , -1
return

Reload:
MsgBox , About to Reload
Reload
ExitApp
return

Exit:
loop
	FileAppend , % A_TickCount . "`n" , Debug.txt
ExitApp
return

And again, the documentation does state that OnExit can be interrupted by Reload, so this isn't something that is broken or needs fixing.

My suggestion was merely to advise in the documentation that execution continues after Reload.
pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Execution continues after Reload command

01 Dec 2019, 19:50

So if Reload is used on its own (without ExitApp after it) Reload will automatically call the OnExit routine, and crucially, will NOT interrupt it, instead allowing the OnExit routine to run for a few seconds, after which this ahk dialogue is posted

Image

Couple of issues:

1. If Reload failed altogether, say due to blockage by antivirus, then the current instance of the script will be left running
2. I don't want the dialogue posted

This is my solution for now

Code: Select all

MsgBox Starting
OnExit("ExitApp")
ExitApp , 2  ;use this instead of Reload
return

ExitApp(ExitReason, ExitCode){
	
	If (ExitCode = 0){
		;do stuff
	}
	If (ExitCode = 1) {
		;do stuff
	}
	If (ExitCode = 2){ 
		;do stuff
		Reload
	}
}
guest3456
Posts: 3134
Joined: 09 Oct 2013, 10:31

Re: Execution continues after Reload command

01 Dec 2019, 22:22

pneumatic wrote:
01 Dec 2019, 19:50
So if Reload is used on its own (without ExitApp after it) Reload will automatically call the OnExit routine, and crucially, will NOT interrupt it, instead allowing the OnExit routine to run for a few seconds, after which this ahk dialogue is posted
...
This is my solution for now
thanks, so i guess Reload inside the exit routine will not recursively call it

pneumatic
Posts: 299
Joined: 05 Dec 2016, 01:51

Re: Execution continues after Reload command

02 Dec 2019, 18:58

guest3456 wrote:
01 Dec 2019, 22:22
so i guess Reload inside the exit routine will not recursively call it

As long as ExitApp,2 isn't in the autoexec section it won't.

If you're concerned about there being no ExitApp inside ExitApp(), it's is not needed as ahk automatically does that, even if a runtime error occurs during it.

However execution will continue after the Reload inside ExitApp() so if you've put any final stuff at the bottom of ExitApp() it will be executed.
extateme
Posts: 3
Joined: 04 Dec 2019, 15:24

Re: Execution continues after Reload command

11 Dec 2019, 05:42

I think it works fine, you are just doing something wrong.
HiDe Techno Tips
Posts: 9
Joined: 20 Aug 2020, 05:58

Re: Execution continues after Reload command

20 Aug 2020, 06:13

How about this?

Code: Select all

; Script here

  Reload
  Goto last

 ; Script here

 last:
                     ; Nothing is executed
 ; End of Script. Nothing after this.
HiDe Techno Tips
Posts: 9
Joined: 20 Aug 2020, 05:58

Re: Execution continues after Reload command

20 Aug 2020, 08:06

HiDe Techno Tips wrote:
20 Aug 2020, 06:13
How about this?

Code: Select all

; Script here

  Reload
  Goto last

 ; Script here

 last:
                     ; Nothing is executed
 ; End of Script. Nothing after this.
I have tested.
This works if the "Reload" command is not inside any function.

But this works everywhere:

Code: Select all


F1::                         ; F1 will start the test function.
test()

test(){
Reload
Gosub last                   ; Gosub can jump out of function but Goto cannot. So, use Gosub.
FileAppend Fail, debug.txt   ; Fail if this happens.
}

last:
FileAppend Pass, debug.txt   ; Pass if this happens.
                             ; No returning from here. No more script below this.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 4 guests