Jump to content
rudi

Howto kill all *OTHER* Processes using same _Singleton() name?

Recommended Posts

@guinness mentioned in a different thread an Approach, I honestly do not 100% how to use it.

I have a script, that shall run on one Windows box only once.

 

If started more than once, it shall not Exit itself, but kill the other processes using the same _Singleton "$sOccurrenceName".

 

So what I would Need is the PID(s) of the *OTHER* instances, that use the same "$sOccurrenceName". This may be from a different logon ("global\"), and the @scriptname might be different, too. (someone might rename the EXE, for what sensless reason whatsoever...)

 

TIA, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

@AutoItPID will give you the current pid of your script.  Then use ProcessExists in a loop to check if a process of the same name exists with a different pid than the current.  If so use ProcessClose to kill it.

Share this post


Link to post
Share on other sites

I personally use this (found somewhere on this forum)
While Singleton doesn't allow a 2nd instance, this one allows the new one and kills the previous ones

Func _killOriginal($winTitle)
  Local $processID = WinGetProcess($winTitle)
  While WinExists($winTitle) 
    WinClose($winTitle)
    Sleep(10) ; allow window to exit
    ProcessClose($processID)
    Sleep(10) ; allow process to exit
  WEnd
EndFunc   ;==>_killOriginal

 

Share this post


Link to post
Share on other sites

Maybe this UDF can help:

_SingleScript()

Close all executing scripts with the same name and continue.
or
Wait for completion of predecessor scripts with the same name.
or
Exit if other scripts with the same name are executing.
or
Test, if other scripts with the same name are executing.

http://www.autoitscript.com/forum/index.php?showtopic=178681

However, it only works if the script name is identical. But it takes into account xxx.exe and xxx.au3 at the same time.

 

Edited by Exit

App: Au3toCmd              UDF: _SingleScript()                             

Share this post


Link to post
Share on other sites

Hello,

 

thanks for all your replies.

 

My question was not precisely enough:

 

_Singleton() is using a descriptive "NAME", that is fixed compiled into the EXE file.

So that "NAME" will be always the same, even when the EXE file has been renamed.

 

The Process Name, that's addressed through all the Process*() functions is always the *NAME OF THE EXE FILE*.

 

What I'm looking for is a list of all the processes, that "have" the same SingletonName, as defined in that line _Singleton("SingletonName",1) as that one will remain untouched, when the EXE file is renamed.

 

Basically something like a function $aSiToList=_SingletonList("SingletonName"), like it's done by $aProcList=ProcessList("EXE-File-Name")

 

 


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

It doesn't work that way.:no: AutoIt's Singleton creates a mutex, a uniquely-named object within the OS; think of it as a user-named flag, usually initially set to busy by the mutex creator (a process or thread). Any other process attempting to create the exact same object while it exists will fail, in the sense that the handle to the existing mutex is returned, along with an error code. Only the creator (or higher authority) can destroy that mutex. The flag itself has no awareness of anything; no queue of which processes/threads are using it, nothing, so this cannot be returned by the OS either. Secondly, the flag does not prevent any access itself; it is a voluntary signalling device between concurrent threads/processes; each one needs to be aware of its existence and query it to obtain ownership to avoid potential race conditions.

Maybe a better solution is to create a Registry entry to which each instance of your process adds (and removes) its own key (process name) or something.

Share this post


Link to post
Share on other sites

Hi rudi,

just consider that once called (one time per a running script) your done using it.
Here are some clues to make singleton work in a more flexible way  (adjusted to your needs ++)

Spoiler
#AutoIt3Wrapper_Outfile=_Second.exe

#include <Misc.au3>
#include <Array.au3>
#include <Process.au3>

If Not @Compiled Then Exit MsgBox(262144, '', "Remember to first compile !!")
$Singleton = 9999
_QueuSingleton()

Func _QueuSingleton()

    Do
        $Singleton -= 1
    Until _Singleton($Singleton, 1)
    $Singleton += 1
    Local $sMyAutoItTitle = "Unique_" & $Singleton
    AutoItWinSetTitle($sMyAutoItTitle)

    Local $sPN = _ProcessGetName(WinGetProcess($sMyAutoItTitle))

    If $Singleton = 9999 Then
        Local $a
        While 1
            $a = ProcessList($sPN)
            _ArrayColDelete($a, 0)
            $sMsg = '"' & $sPN & " Title : " & $sMyAutoItTitle & '" : ' & UBound($a) - 1 & " PIDS: " & @LF & _ArrayToString($a)
            If UBound($a) > 2 Then
                MsgBox(0, 'Message From first instance', $sMsg, 3)
            Else
                MsgBox(0, 'Message From first instance', $sMsg & _
                        @LF & @LF & "Dont close me yet" & @CRLF & "Go Start a few more instance of me", 5)
            EndIf
        WEnd
    EndIf
    MsgBox(0, $sPN, "Will auto exit in 10 seconds" & @CRLF & @CRLF & "I Am " & $sMyAutoItTitle & @CRLF & "PID :" & @AutoItPID, 3)
    Sleep(7000)
EndFunc   ;==>_QueuSingleton

 

Deye

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ur
      I have used _Singleton function in my script to restrict only one instance to run.
      For testing, I have written below code, and triggered multiple instances.
      #include <Misc.au3> MsgBox(0,"",@ScriptName) if _Singleton(@ScriptName, 1) = 0 Then Msgbox(64, @ScriptName, "The program is already running.") Exit Else MsgBox(0,@ScriptName,"No other instances running") EndIf But all are going to else block.
       
      Please suggest.
    • By n3wbie
      i Tried Using Function _single ton
      It Works Like Charm on same computer
      But it is unable to catch up when 2 instance of exe are launched in lan pc
      I would also like to know if there was a way to make particular number of instance work like allow 4-5 instance of same exe to run
    • By TheSaint
      This is just a little helpful script, that I started using a month or so ago (one program so far), with this version having a Restore ability added (so now using with two programs of mine, as of tonight).
      You can find other alternatives or similar scripts here, the most recent being - _SingleScript
      guinness also has something much more elaborate, that is well worth checking out - _SingletonHWID.
      Just place the following at the beginning of your script, and adjust where needed and wanted (including Scriptname, variable names and MsgBox magic numbers, etc). Perhaps you should read and use the following - Best coding practices ... unlike my slack hobby programmer self (not always).
      #include <Misc.au3> Global $ans, $exe, $handle, $pid, $script, $Scriptname, $status, $w, $wins ; Assign the program name (window title). $Scriptname = "Update Mp3 Artwork v7.9 (updated December 2015)" ; The following line both sets the unique ID of the current program, plus queries if a program with the same ID is running already. $status = _Singleton("update-mp3artwork-thsaint", 1) If $status = 0 Then ; A program with the same ID is already running. ; Assign a PID for previous script, based on current executable name, whether current script is compiled or not, and assign the executable name. If @Compiled = 1 Then $pid = ProcessExists(@ScriptName) $exe = @ScriptName Else $pid = ProcessExists("AutoIt3.exe") $exe = "AutoIt3.exe" EndIf ; Assign a PID for current script. $script = @AutoItPID ; $ans = MsgBox(262177, "Close Running Instance Query", _ "'Update Mp3 Artwork' program is already running." & @LF & @LF & _ "Do you want to close it for another instance?" & @LF & @LF & _ "NOTE - If all work has been saved, and you are" & @LF & _ "trying to work on another album, then a click on" & @LF & _ "OK is recommended, else just click CANCEL, to" & @LF & _ "attempt a restore or re-activate of original." & @LF & @LF & _ $pid & " (" & $exe & ") " & $script & @LF & @LF & _ "(will default to CANCEL in 30 seconds)", 30) If $ans = 1 Then ; Attempt to close original instance, and continue with new. If $pid <> $script Then ProcessClose($pid) Else MsgBox(262192, "Close Error", "OK process failed!", 0) Exit EndIf ElseIf $ans = 2 Or $ans = -1 Then ; Attempt to retore and activate a non-active or minimized window. ; Get a list of all running programs with the same name. $wins = WinList($Scriptname, "") ; Loop through found instances and compare PID's, restoring or re-activating any match. For $w = 1 to $wins[0][0] $handle = $wins[$w][1] If WinGetProcess($handle, "") = $pid Then WinSetState($handle, "", @SW_RESTORE) WinActivate($handle, "") ExitLoop EndIf Next Exit EndIf EndIf Enjoy!
      P.S. I have a couple of programs that I regularly forget are minimized while working with another related one, so this saves me some mucking around to close and restart or restore etc. Pretty basic, but does the job well so far.
    • By explorerpl
      Hi. I'm a teacher and I do a lot of tutorials and other presentations on my computer. I've developed a tool using AutoIt and Adobe AIR to display all the shortcuts I use while I'm presenting. According to the forum rules this would mean that I've developed a keylogger, so I can't show any of the code, but I'm still hoping someone will help me solve an issue I'm having - a memory leak (or at least I think that's it).
      I can see the application is taking up more and more memory, but it never goes super crazy. I think it was at 25 MB at one point and that was it. However I see that the longer the application is running less responsive it is. It doesn't capture all the events, or it simply lags. 
      I'm using AssocArrays and _MouseOnEvent UDFs, _WinAPI_SetTimer, _WinAPI_SetWindowsHookEx, _Singleton and TCP. I've done some research before posting this and I know there are some issues in special cases, but all solutions were "code specific". Since I can't post any of the code I couldn't respond in those threads. Other than that it really doesn't seem to be the problem with any of the UDFs, so my question is:
      Is this a memory leak? If so how can I find it and remove it? What to do to avoid it in the future.
      I understand that declaring variables over and over (something in the timer) may be the cause of this, so according to what I've read on the forum I've changed the variables to Global and moved them outside the functions. That way they are only declared once, and then only values are being reassigned. That unfortuantly didn't help. Is there anything else I could do or look for?
      BTW - I've used Adobe AIR to create a nice UI. If someone want's to create something similar UEZ was kind enough to share his code of creating such GUI with nice antialiased labels. 
      https://www.autoitscript.com/forum/topic/178366-adobe-air-like-window/#comment-1280587

    • By TheDcoder
      Hello , Please look at the code below:
      ; This is not the real script that I use for my project!!! This decoy script helps you understand better ;) #include "Misc.au3" Global $sProgramIdentifier Func Test($sIdentifier) $sIdentifier = $sProgramIdentifier Switch _Singleton($sProgramIdentifier, 1) Case 0 MsgBox(64, "Testing, Testing", "Its working!!") Case Else MsgBox(64, "Testing, Testing", "Its not working!!") EndSwitch EndFunc Test("Lick the lemon") Save this to a file and run it, you will get a MsgBox telling "Its not working!!"
      Again, Run the script a second-time without closing the first MsgBox and you will again get a MsgBox telling the same thing!! This was not supposed to happen
      How can I get the MsgBox telling that it works ?
      Thanks In Advance, TD
×
×
  • Create New...