Jump to content

Single instance of a process


Buzbee
 Share

Recommended Posts

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

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.

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 Gude
How 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

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 by BetaLeaf

 

 

Link to comment
Share on other sites

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 Gude
How 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

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

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

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])
Next

And 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 by jdelaney
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

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.  

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...