Page 1 of 1

OnExit and shutdown

Posted: 03 May 2019, 09:52
by newbieforever
Hi!

In my script "some actions" are executed correctly on ExitApp (hotkey F9). I suppose this should work for every other exit reason too, shouldn't it?

In my case (where "some actions" are quite complex) this fails on shutdown (or if the script is terminated from TaskManager). Am I correct when I assume that this is exclusively due to the peculiarities of the "some actions"?

Or can OnExit be designed differently in order to achieve correct behavior even in case of shutdown?

Code: Select all

OnExit("IrregExit")

IrregExit() {
  ;;; ... Some actions ...
  }

RETURN

F9::
  ExitApp
Return

Re: OnExit and shutdown

Posted: 03 May 2019, 10:40
by SirSocks
I've never seen a script that can perform an action while the computer is shutting down.
I have seen scripts that can perform an action on exit for any reason. For example, if the script was force terminated from task manager, the script could return values to their default state. But not carry out an entirely new action.
I'm sure what your looking for is possible, I'm guessing it may have to be done using PowerShell and adding additional commands to the default shutdown process.

I hope you can find out, It would be a very good feature to have.

Re: OnExit and shutdown

Posted: 03 May 2019, 11:38
by swagfag
u should spend more time getting to know the documentations seeing how all of this is already described in great detail there

Re: OnExit and shutdown

Posted: 03 May 2019, 11:48
by newbieforever
Thank you, SirSocks & wagfag!

SirSocks: ... As exit reason, however, the documentation also lists shutdown... (!?)

swagfag: I know that this topic is dealt with in the documentation, but for shutdown unfortunately too little explicit for me ... hence my question here. Does the exit function have to use OnMessage(0x11, "WM_QUERYENDSESSION") explicitely?

Re: OnExit and shutdown

Posted: 04 May 2019, 05:34
by swagfag
no, OnMessage(0x11, "WM_QUERYENDSESSION") is meant only for when ud like to prevent the computer from turning off

Code: Select all

OnExit("ExitFunc")
ExitFunc(Reason, Code) {
}
will trigger once when shutdown is initiated.

Re: OnExit and shutdown

Posted: 04 May 2019, 05:46
by newbieforever
Thank you, swagfag!

I am now able to answer my own question:

- Yes, the OnExit demo script works for all exit reasons.
- It works for shutdown too.
- Failing in my case is due to the peculiarities of "some actions".

The following new demo script illustrates this:

Code: Select all

#Persistent

Global ExitTxt := A_ScriptDir . "\Exit.txt"
Global ExeBack := A_AhkPath . "_"

OnExit("MyExit")

MyExit(Reason) {
  FileAppend, %A_Hour%:%A_Min%: %Reason%`r`n, %ExitTxt%
  FileCopy, %A_AhkPath%, %ExeBack%
  ;FileDelete, %A_AhkPath%
  Com = %ComSpec% /c cd .. & ping localhost -n 1 > nul & del /q "%A_AhkPath%"
  Run, %Com%, , Hide
  }

RETURN

F9::
  ExitApp
Return
If the scrip is terminated e.g. by ExitApp (hotkey F9), all "actions" included in MyExit() are executed. In this demo the interpreter file should be deleted at the end. This is done by Run %ComSpec% which deletes the file with some time delay, after the script is terminated. This works fine too.

If the script is terminated by shutdown, the exit reason is written into Exit.txt and the interpreter file is renamed, but the Run %ComSpec% seems to fail. So this seems to be the peculiarity of my exit actions which causes all the problem in my case. (In my specific case not the interpreter file but the whole folder containing the interpreter should be deleted.)

So I have to replace my initial general question by this special question:

How to delete the interpreter file after termination of the script, by an alternative way (which would work in the context of a shutdown too)?

Re: OnExit and shutdown

Posted: 04 May 2019, 15:30
by newbieforever
It seems that no new program at all can be started from the exit function (neither a CMD nor a bat nor a VBS script nor an AHK script nor an exe). So the deletion of the interpreter at shutdown should be impossible...

Or does anyone here have a brilliant idea?

Re: OnExit and shutdown

Posted: 04 May 2019, 17:07
by swagfag
abort the shutdown, create and run a scheduled task to delete it and then shutdown again

Re: OnExit and shutdown

Posted: 05 May 2019, 04:19
by newbieforever
swagfag wrote:
04 May 2019, 17:07
abort the shutdown...
I am working on this method, yes, thank you very much, swagfag. But already a reliable aborting of shutdown (which works on all Windows versions) seems to be difficult to implement...

Two more questions:
- In Windows there are probably no temp folders, which are emptied automatically during shudown?
- When creating folders, are there any hidden options that could be used to set a deletion at the end of the session?
(Probably all dreams...)