Jump to content

Run code when script is killed?


IAMK
 Share

Recommended Posts

IF it s killed with alt F4 you setup a shordcut. < Never tryed to Set hotkey altF4 may you can try and feedback us here ?

HotKeySet("!"&"{F4}", "Escape")

Func Escape ()
      ConsoleWrite ("Escape ()"&@CRLF)
      $ExitYesNo = MsgBox ($MB_YESNO , "Quitter?" , "Voulez-vous quitter ?")
      If $ExitYesNo = "6" Then
            Exit
      EndIf
      ;EndIf
EndFunc ; Escape

If this is not working this is the other solution that will work i guess : 

You make separate script with just the detection of ALT + F4 like that : 

#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")

;If you use that piece of code i guess you will fall in the same progblem as me. If the ALT key only is pressed it will still start the log. 
;SO you have to add a loop like that to remove this issue : 
 If _IsPressed ("12",$hDLL) = 1 Then
      Do
         Sleep (100)
         $Timer += 100
         If $Timer = 3000 Then ExitLoop
      Until _IsPressed ("73") ;Until F4 pressed do nothing. If user push ALT more than 3 sec it will do the code below
EndIf

;Then you check for alt + F4 with this sample : 
If _IsPressed ("12",$hDLL) = 1 + _IsPressed ("73",$hDLL) = 1 Then
 YourStuff
EndIF

If it s killed by the tray yes you still can i dont remember how. But i know you can. 

There is something like TrayOnEvent or somthing like that you can search. It is : TraySetOnEvent : "Tray management" in the help file :) 

If it s killed by GUI message you add your code in the leaving function.

While (1)
   $msg = GUIGetMsg ()
   Switch $msg
         Case $GUI_EVENT_CLOSE
            Escape (); Exit
   EndSwitch
WEnd

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

If you're just trying to capture closing the program naturally you can use OnAutoItExitRegister

If you're referring to killing a task from task manager, the only way would be to have two processes running at the same time, both monitoring each other

So:

Startup script starts Main script and monitors when process is closed and writes the log when Main.exe process is closed.
Main script checks Startup script is always running, using AdLibRegister to check and re-start Startup script if it's closed.
 

 

Link to comment
Share on other sites

I guess he look to know when and how to manage user’s utilisation . Didn’t Thought about task manager n1. :)

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

Link to comment
Share on other sites

Hi all,

Thanks Subz for indicating OnAutoItExitRegister, didn't know that one.
Like Caramen, I guess OP would like to supervise a user exiting the script abnormally.

So IAMK, here is a script I just wrote that could help you, tweak it as you wish.
The following 1st pic will display if you click on exit in systray, during the 5s the script will run.
I tested the other situations too (logoff, windows shutdown) and the log file did a fine job (2nd pic)
Good luck :)

OnAutoItExitRegister.png.c776949dd0dd884e91dc88883d2af44e.png   5bf83e8528b5f_Howdidscriptend(log).jpg.cec647e1910f4ffb4aec2e794ee48010.jpg

#include <Date.au3>
#include <MsgBoxConstants.au3>

OnAutoItExitRegister("How_Did_Script_End")

MsgBox($MB_TOPMOST, "Don't touch please", "This script will end in 5 seconds", 5) ; timeout 5s

Func How_Did_Script_End()
    Local $Date_Time = _Now()
    Local $sMsg = ""

    Switch @exitMethod
        Case 0
            $sMsg = "Natural ending"
        Case 1
            $sMsg = "Exit function encountered"
        Case 2
            $sMsg = "Click on Exit in Systray"
        Case 3
            $sMsg = "User logoff"
        Case 4
            $sMsg = "Windows Shutdown"
        Case Else
            $sMsg = "Unknown reason"
    EndSwitch

    MsgBox($MB_TOPMOST, "How did script end ?", $sMsg)

    If @exitMethod > 1 Then
        Local $sLogFile = @ScriptDir & "\Log_to_be_deleted.txt"
        FileWrite($sLogFile, $Date_Time & @CRLF & _
          "Script " & @ScriptName & " did not end normally : " & @CRLF & _
          "@exitMethod = " & @exitMethod & " (" & $sMsg & ")" & @CRLF & @CRLF)

        ; Display the log file
        ShellExecute($sLogFile)
    EndIf
EndFunc   ; ==> How_Did_Script_End

 

Edited by pixelsearch
re-uploaded 1 pic
Link to comment
Share on other sites

@pixelsearch Thanks, that's a great script.

@Subz USUALLY, my hotkeys are used to kill the script, but in some cases I do need it for Task Manager, as that's a possible way it gets killed (if someone doesn't know to use the hotkey).
If I need system performance monitoring, will your solution affect much? Or is it negligible?

Link to comment
Share on other sites

Killing a process in Task Manager will kill the process so it can't perform any tasks, however in the past I've had to use something like below to capture the exit code.

Startup Script
This script will start the Main Script and sit idle until the Main Script is closed normally or closed abnormally.

;~ Startup Script
#NoTrayIcon
#include <WinAPI.au3>

Global Const $PROCESS_QUERY_INFORMATION = 0x0400

;~ Process Name
Global $sProcessName = "MainScript.exe"
;~ Process Pass
Global $sProcessPath = @ScriptDir & "\MainScript.exe"

;~ Get Process Id
If ProcessExists($sProcessName) = 0 Then
    $iProcessId = Run($sProcessPath)
Else
    $iProcessId = ProcessExists($sProcessName)
EndIf

;~ Get Process Handle
Global $hProcess = _WinAPI_OpenProcess($PROCESS_QUERY_INFORMATION, 0, $iProcessId)

;~ Wait until Process is closed
While ProcessExists($iProcessId)
    Sleep(100)
WEnd

;~ Get Process ExitCode
Global $iProcessExitCode = _ProcessGetExitCode($hProcess)

;~ Display Process ExitCode
MsgBox(4096, "MainScript Closed", "MainScript.exe was closed with ExitCode: " & $iProcessExitCode)

;~ Close Process Handle
_WinAPI_CloseHandle($hProcess)

;~ _ProcessGetExitCod by @therks
Func _ProcessGetExitCode($hProc) ;Get process exit code from handle
    Local $t_ExitCode = DllStructCreate('int')
    Local $avRET = DllCall('kernel32.dll', 'int', 'GetExitCodeProcess', 'ptr', $hProc, 'ptr', DllStructGetPtr($t_ExitCode))
    If @error Then
        Return SetError(1, 0, 0)
    Else
        Return DllStructGetData($t_ExitCode, 1)
    EndIf
EndFunc

Main Script
This is the main script where work is done, it checks periodically to ensure the Startup Script is still running so that it can capture the ExitCode.

;~ MainScript
#include <GUIConstantsEx.au3>

Global $sProcessName = "Startup.exe"
Global $sProcessPath = @ScriptDir & "\Startup.exe"

Example()

Func Example()
    Local $hGUI = GUICreate("Example")
    Local $idExitScript = GUICtrlCreateButton("OK", 310, 370, 85, 25)

    GUISetState()

    ;~ Checks every 250ms that Startup.exe is still running
    AdlibRegister("_StartupScript")

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idExitScript
                Exit 1
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

;~ Checks to see if Startup.exe process is still running, re-runs if process is not found.
Func _StartupScript()
    If ProcessExists("Startup.exe") = 0 Then
        Run(@ScriptDir & "\Startup.exe")
    EndIf
EndFunc

 

Link to comment
Share on other sites

You can log the fact that taskmng is run too then you can log where the user click inside it. and also if the process exists i am sure there is a way with this.

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...