Trong Posted August 19, 2021 Share Posted August 19, 2021 (edited) 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 August 19, 2021 by VIP fix ProcessExists() Regards, Link to comment Share on other sites More sharing options...
Developers Jos Posted August 19, 2021 Developers Share Posted August 19, 2021 (edited) 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 August 19, 2021 by Jos Trong 1 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 More sharing options...
Trong Posted August 19, 2021 Author Share Posted August 19, 2021 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 More sharing options...
Trong Posted August 19, 2021 Author Share Posted August 19, 2021 15 minutes ago, Jos said: I also don't understand why you have this test as that should always be true..no?: If ProcessExists($exeScriptName) Then Jos Thank you very much for noticing my error, it has been fixed! Regards, 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