Jump to content

Detect Windows SessionEnding/shutdown/Restart ?


Recommended Posts

hey

I wonder if there is a possibility to do this in Autoit ? I've to make sure that my application is closed properly to prevent crahses / errors

in VB.NET I was able to do it by :

                AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf myfunctions_end_mywork
 

I found an old example here : but its not working 

 

 

Link to comment
Share on other sites

you need to register a function to be called when the script receives the termination signal from the system. in this function, you can detect cause of termination by a built-in macro. this is how it can be done:

OnAutoItExitRegister('OnExit')

; your script here (includes, globals, main loop, functions)
While True
    Sleep(10)
WEnd

Func OnExit()
    Switch @exitMethod
        Case 0, 1, 2
            ; do stuff related to normal exit
        Case 3
            ; do stuff related to log off
        Case 4
            ; do stuff related to shutdown/restart
    EndSwitch
EndFunc   ;==>OnExit

look at the help file for relevant function OnAutoItExitRegister() and macro @exitMethod.

keep in mind that the exit function should be very short and finish its job quickly, or Windows will kill your script if it does not respond in due time. so don't start opening IE windows or perform database maintenance here. do some logging, raise a flag, no more.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

On 2/5/2016 at 8:10 PM, orbs said:

you need to register a function to be called when the script receives the termination signal from the system. in this function, you can detect cause of termination by a built-in macro. this is how it can be done:

OnAutoItExitRegister('OnExit')

; your script here (includes, globals, main loop, functions)
While True
    Sleep(10)
WEnd

Func OnExit()
    Switch @exitMethod
        Case 0, 1, 2
            ; do stuff related to normal exit
        Case 3
            ; do stuff related to log off
        Case 4
            ; do stuff related to shutdown/restart
    EndSwitch
EndFunc   ;==>OnExit

look at the help file for relevant function OnAutoItExitRegister() and macro @exitMethod.

keep in mind that the exit function should be very short and finish its job quickly, or Windows will kill your script if it does not respond in due time. so don't start opening IE windows or perform database maintenance here. do some logging, raise a flag, no more.

thanks a lot :D

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

×
×
  • Create New...