Jump to content

"Enforce Single Instance" The version replaces it with the latest Instance.


Trong
 Share

Recommended Posts

UDF:

_EnforceSingleInstance() ;~ PUT IT AT THE TOP OF YOUR SCIPT !

Func _EnforceSingleInstance()
    Local $exeScriptName = @ScriptName
    If (@Compiled = 0) Then $exeScriptName = StringRegExpReplace(@AutoItExe, "^.*\\", "")
    Local $regKey = "HKEY_CURRENT_USER\SOFTWARE\TRONG_UDF\" & $exeScriptName, $regValue = "Last_PID"
    Local $regReturn = RegWrite($regKey, $regValue, "REG_SZ", @AutoItPID)
    If ($regReturn = 0) Then Exit
    Sleep(100)
    Local $Last_PID = RegRead($regKey, $regValue)
    If (@AutoItPID <> $Last_PID) Then Exit
    Local $aProcessList = ProcessList($exeScriptName)
    For $i = 1 To $aProcessList[0][0]
        If ($aProcessList[$i][1] <> @AutoItPID) Then ProcessClose($aProcessList[$i][1])
    Next
    RegDelete($regKey)
;~ by DAO VAN TRONG - TRONG.LIVE
EndFunc   ;==>_EnforceSingleInstance

 

Edited by VIP
fix ProcessExists()

Regards,
 

Link to comment
Share on other sites

  • Developers

Could be me but what is the purpose of this section:

Local $regKey = "HKEY_CURRENT_USER\SOFTWARE\TRONG_UDF\" & $exeScriptName, $regValue = "Last_PID"
    RegWrite($regKey, $regValue, "REG_SZ", @AutoItPID)
    Sleep(100)
    Local $Last_PID = RegRead($regKey, $regValue)
    If @AutoItPID <> $Last_PID Then Exit

The If is never true and why Read a Reg value you just wrote yourself?
Isn't this the exact same thing:?

Local $regKey = "HKEY_CURRENT_USER\SOFTWARE\TRONG_UDF\" & $exeScriptName, $regValue = "Last_PID"
    RegWrite($regKey, $regValue, "REG_SZ", @AutoItPID)

 

I also don't understand why you have this test as that should always be true..no?:

If ProcessExists($exeScriptName) Then

 

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The steps for this function are:
1. Write the current program's PID to the Registry.
2. Pause the program, so that other programs can start more slowly overwrite the value.
3. Check the initial PID value, if it is different, exit because another program starts later.
4. Check once again if there exist applications with the same name running.
5. Delete the temporary reg key just written above.

 

7 minutes ago, Jos said:

The If is never true and why Read a Reg value you just wrote yourself?
Isn't this the exact same thing:?

Local $regKey = "HKEY_CURRENT_USER\SOFTWARE\TRONG_UDF\" & $exeScriptName, $regValue = "Last_PID"
    RegWrite($regKey, $regValue, "REG_SZ", @AutoItPID)

Jos

If the application runs normally then the chance of an error is 0%!

Regards,
 

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