Buzbee Posted January 13, 2016 Share Posted January 13, 2016 Im looking for a way to launch an executable (not the current script), get the pid, and close any other instance of that executable that does not match the pid!Any leads in the right direction or snippets around?I found something similar but it seems the user didnt care what instance of the process was closed Link to comment Share on other sites More sharing options...
BrewManNH Posted January 13, 2016 Share Posted January 13, 2016 ProcessList using the name of the executable will return all instances of the program that is running. Then you can search through the array for any instance that doesn't match the PID and then close it with ProcessClose. Buzbee 1 If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
EmilyLove Posted January 13, 2016 Share Posted January 13, 2016 (edited) Here you go.Global $WhitelistSinglePID = "15616" Global $Process = "Notepad.exe" ;Set Notepad.exe as the Process we are checking Global $Processes = ProcessList($Process);get all relevant Processes For $i = 1 To $Processes[0][0]; For Each found unique PID If Not ($Processes[$i][1] = $WhitelistSinglePID) Then ProcessClose($Processes[$i][1]);Close Process if it is not the whitelisted process Next Edited January 13, 2016 by BetaLeaf Buzbee 1 Link to comment Share on other sites More sharing options...
BrewManNH Posted January 13, 2016 Share Posted January 13, 2016 BetaLeaf, I'm guessing you've never heard of the concept of "Teach a man to fish"? Take special note of the red text in my signature for an extension to that idea. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
jdelaney Posted January 13, 2016 Share Posted January 13, 2016 Check out this function in helpfile as well:Run() IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
EmilyLove Posted January 13, 2016 Share Posted January 13, 2016 Yea well here's the thing, I was already this exact code in many of my projects. I know it works. Also, while I do agree with you, the user did go thru the effort to search before posting, so I felt sharing what I already knew would have been find in this case. Link to comment Share on other sites More sharing options...
jdelaney Posted January 13, 2016 Share Posted January 13, 2016 It give a good baseline for the loop part. I don't like the static variable for the pid though. PID will never be static. It must be obtained dynamically. IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
EmilyLove Posted January 13, 2016 Share Posted January 13, 2016 It give a good baseline for the loop part. I don't like the static variable for the pid though. PID will never be static. It must be obtained dynamically.It is just an example post to get the op in the right direction. Link to comment Share on other sites More sharing options...
jdelaney Posted January 13, 2016 Share Posted January 13, 2016 (edited) Might as well actually demonstrate it then:$Process = "Notepad.exe" ; populate a few processes to be closed later Run($Process) Run($Process) Run($Process) ; the actual "process" you will initiate in your script $iPid = Run($Process) $Processes = ProcessList($Process) For $i = 1 To $Processes[0][0] If Not ($Processes[$i][1] = $iPid) Then ProcessClose($Processes[$i][1]) NextAnd now I'm getting bored #include <WinAPI.au3> $Process = "Notepad.exe" ; populate a few processes to be closed later For $i = 1 To 5 Run($Process) Next ; the actual "process" you will initiate in your script $iPid = Run($Process) ConsoleWrite($iPid & " will not be closed" & @CRLF) Sleep(1000) $a = WinList("[CLASS:Notepad]") Local $sPid, $x = 0, $y = 0 For $i = UBound($a)-1 To 1 Step -1 _WinAPI_GetWindowThreadProcessId($a[$i][1],$sPid) ControlSetText($a[$i][1],"",15,"PID=[" & $sPid & "]") WinMove($a[$i][1],"",$x,$y) $x+=10 $y+=80 Next MsgBox(1,1,$iPid & " will not be closed") $Processes = ProcessList($Process) For $i = 1 To $Processes[0][0] If Not ($Processes[$i][1] = $iPid) Then ProcessClose($Processes[$i][1]) Next Edited January 13, 2016 by jdelaney EmilyLove 1 IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
Buzbee Posted January 17, 2016 Author Share Posted January 17, 2016 Thank you BrewManNH, BetaLeaf, and jdelaney for the help. Func LoadPID() SplashTextOn($app_name & " Monitor", $app_name & " not running!" & @CRLF & "Please wait for it to reload!", 250, 75, -1, -1, $DLG_TEXTVCENTER, "", 12) Global $iPID = Run($app_path) Sleep($tick*1000) If ProcessExists($proc_name) Then Sleep($tick*1000) SplashOff() EndIf EndFunc Func MonPID() Global $proc_list = ProcessList($proc_name) For $i = 1 To $proc_list[0][0] If Not ($proc_list[$i][1] = $iPID) Then ProcessClose($proc_list[$i][1]) Next EndFunc Im currently using these functions to run and monitor the process. Had a problem properly getting the PID of the process if it was running before the script executes, to work around that I check for the process at script launch and if its running it will close the process and then run LoadPID() Everything I am trying to accomplish has been working, with the exception of calling to execute "script2.au3" script from "script1.au3". It works in the editor just fine but it will not run "script2.au3" from a compiled "script1.exe". For now I compile both scripts and call to the exe and not the au3. EmilyLove 1 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 17, 2016 Moderators Share Posted January 17, 2016 Buzbee, Quote it will not run "script2.au3" from a compiled "script1.exe" If you are using a recent version of AutoIt you need to add the following directive to allow a compiled exe to run an external script: #pragma compile(AutoItExecuteAllowed, True) By default this functionality is now disabled. M23 Buzbee 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
EmilyLove Posted January 17, 2016 Share Posted January 17, 2016 No Problem, Buzbee. Glad to help. 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