Stammenator Posted May 26, 2023 Share Posted May 26, 2023 I have this little script that essentially just moves desktop icons to my desired position, and then if they are moved, they "snap" back into place. I have it setup to run the .exe via login script. I run into some issues if there are two instances of this script running at the same time. How can I make an exit parameter to detect if there are two instances of this script running, and close one of the two (the 'longer running' one would be best, but either would work). Here is the code: #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=..\Downloads\Logo-for-Pax-Machine-in-RGB.ico #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GuiListView.au3> Local $arrShortcutNames[19] = ["Company Postings", "Redacted", "Redacted Database", "Redacted", "Mobile Redacted", "Redacted Private", "Redacted Records", "Redacted Log Files", "Mfgx-edge-v3", "Search by Redacted in Redacted", "Redacted Schedule", "Redacted UX", "Production Card", "Calculator", "Certification", "Redacted", "CRedacted Log Files", "Notepad", "Redacted"] Local $arrXPos[19] = [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 116, 116, 116, 116, 116, 116, 116, 116, 116] Local $arrYPos[19] = [29, 105, 192, 278, 364, 449, 535, 621, 707, 793, 29, 105, 191, 277, 363, 535, 621, 707, 793] While 1 For $i = 0 To UBound($arrShortcutNames) - 1 _SetShortcutPos($arrShortcutNames[$i], $arrXPos[$i], $arrYPos[$i]) sleep(100) Next Sleep(1000) WEnd Func _SetShortcutPos($Name = "", $X = 0, $Y = 0) Local $Desktop = ControlGetHandle("[CLASS:Progman]", "", "[CLASS:SysListView32;INSTANCE:1]") Local $Pos = _GUICtrlListView_FindInText($Desktop, $Name) _GUICtrlListView_SetItemPosition($Desktop, $Pos, $X, $Y) EndFunc Thank you! Link to comment Share on other sites More sharing options...
ioa747 Posted May 26, 2023 Share Posted May 26, 2023 (edited) ;Put on top of your script the follow command, to avoid running it multiple times! ;You probably also want to test whether the script is already running to avoid running it multiple times! ;If _Singleton(@ScriptName, 1) = 0 Then Exit #include <Misc.au3> _Singleton(@ScriptName) Edit: something similar210280-_guictrllistview_findintext-and-windows-10/#comment-1518544 Edited May 26, 2023 by ioa747 correction I know that I know nothing Link to comment Share on other sites More sharing options...
Solution TheXman Posted May 26, 2023 Solution Share Posted May 26, 2023 (edited) 3 hours ago, ioa747 said: If _Singleton(@ScriptFullPath, 1) = 0 Then Exit Using @ScriptFullPath as the _Singleton()'s occurrence name will not work because it will contain one or more prohibited backslashes (\), which will make the _Singleton() function fail and return 0. That syntax would exit upon every invocation. If you want to use a macro, then @ScriptName should be unique enough. If all you want the duplicate process to do when launched is exit, then you just need: #Include <misc.au3> _Singleton(@ScriptName) You only need to set $iFlag to 1 if you want to capture the fact that a duplicate process has been launched, for example, if you want to show a message box before exiting. By default ($iFlag = 0), upon executing the function above, the script will immediately exit with an exit code of -1 (if a duplicate script is already running). The example below shows when using the @ScriptFullPath macro, if you were checking the return of _Singleton(@ScriptFullPath, 1) to 0 in order to exit, then it would have exited upon every execution of the function. When using @ScriptName, only executions after the initial execution would exit. #include <Misc.au3> ConsoleWrite("_Singleton(@ScriptFullPath, 1) = 0: " & (_Singleton(@ScriptFullPath, 1) = 0) & @CRLF) ConsoleWrite("_Singleton(@ScriptFullPath, 1) = 0: " & (_Singleton(@ScriptFullPath, 1) = 0) & @CRLF) ConsoleWrite(@CRLF) ConsoleWrite("_Singleton(@ScriptName, 1) = 0: " & (_Singleton(@ScriptName, 1) = 0) & @CRLF) ConsoleWrite("_Singleton(@ScriptName, 1) = 0: " & (_Singleton(@ScriptName, 1) = 0) & @CRLF) Console output: _Singleton(@ScriptFullPath, 1) = 0: True _Singleton(@ScriptFullPath, 1) = 0: True _Singleton(@ScriptName, 1) = 0: False _Singleton(@ScriptName, 1) = 0: True Edited May 26, 2023 by TheXman ioa747 and Stammenator 2 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman 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