IAMK Posted November 23, 2018 Share Posted November 23, 2018 Is it possible to have a piece of code which is forced to run when the script is force killed? I want to have a FileWrite() in this situation to do some logging to a textfile. Link to comment Share on other sites More sharing options...
caramen Posted November 23, 2018 Share Posted November 23, 2018 (edited) 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 November 23, 2018 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Subz Posted November 23, 2018 Share Posted November 23, 2018 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. IAMK and pixelsearch 2 Link to comment Share on other sites More sharing options...
caramen Posted November 23, 2018 Share Posted November 23, 2018 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Nine Posted November 23, 2018 Share Posted November 23, 2018 It's never a good idea to force kill any process unless it is totally frozen. You never know what the process is doing when you force kill it ! “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
pixelsearch Posted November 23, 2018 Share Posted November 23, 2018 (edited) 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 expandcollapse popup#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 May 2, 2022 by pixelsearch re-uploaded 1 pic FrancescoDiMuro and IAMK 2 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted November 23, 2018 Share Posted November 23, 2018 Very nice example @pixelsearch Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
pixelsearch Posted November 23, 2018 Share Posted November 23, 2018 Thx @FrancescoDiMuro , glad you liked it Link to comment Share on other sites More sharing options...
IAMK Posted November 23, 2018 Author Share Posted November 23, 2018 @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 More sharing options...
Subz Posted November 24, 2018 Share Posted November 24, 2018 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. expandcollapse popup;~ 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 More sharing options...
caramen Posted November 24, 2018 Share Posted November 24, 2018 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now