Burgaud Posted February 17, 2021 Share Posted February 17, 2021 Currently, I use HotKeySet to declare a script's existence to the other scripts. example: if HotKeySet("^!+x", "_DoNothing")=0 then ;Ctrl-ALT-Shift-X exit(0) endif func _DoNothing() endfunc If the key combo already exists, then said script will exit. THEN I found _Singleton function _Singleton ( $sOccurrenceName [, $iFlag = 0] ) My questions are: 1. what is the value for $sOccurenceName? Is it any unique text I like to associate with said script? example perhaps @ScriptName or @ScriptFullPath? Or is there some format like PID or something? 2. If I have two different scripts and both uses _Singleton("MyScript",1), what would happen? Link to comment Share on other sites More sharing options...
water Posted February 17, 2021 Share Posted February 17, 2021 5 hours ago, Burgaud said: Is it any unique text I like to associate with said script? Exactly. _Singleton uses parameter 1 to identify a script instance. It doesn't matter which script uses the identifier. Burgaud 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Burgaud Posted February 18, 2021 Author Share Posted February 18, 2021 I tried to use _Singleton in a loop #include <Misc.au3> #include <MsgBoxConstants.au3> while 1 MsgBox($MB_SYSTEMMODAL, "OK", _Singleton("Test _Singleton", 1), 1) wend and realized, the 3rd and beyond will always give me 0. Is there a way for Singleton to be used to monitor if another latter script uses the same identifier? Link to comment Share on other sites More sharing options...
Nine Posted February 19, 2021 Share Posted February 19, 2021 _Singleton use mutex (mutual exclusive). Which mean that nobody but the first process can acquire the object. Second can test if it is already acquire, but first process will always failed since it has exclusivity. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
water Posted February 19, 2021 Share Posted February 19, 2021 In my project a lot of instances of my script start at the same time. To make sure only one instance is processing at a time and all other instances wait until the active instances has ended, I use the following approach: ; Add at the top of your script While 1 If _SingletonEX("string to identify this script", 1) = 0 Then Sleep(@AutoItPID * 10) ; Makes sure that not all waiting instances check at the same time which could lead to a loc Else ExitLoop EndIf WEnd ; Modified version of _Singleton - can't tell why the original version didn't work for me Func _SingletonEX($sOccurenceName, $iFlag = 0) Local Const $ERROR_ALREADY_EXISTS = 183 Local $tSecurityAttributes = 0 Local $handle = DllCall("kernel32.dll", "handle", "CreateMutexW", "struct*", $tSecurityAttributes, "bool", 1, "wstr", $sOccurenceName) If @error Then Return SetError(@error, @extended, 0) Local $lastError = DllCall("kernel32.dll", "dword", "GetLastError") If @error Then Return SetError(@error, @extended, 0) If $lastError[0] = $ERROR_ALREADY_EXISTS Then DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $handle[0]) If @error Then Return SetError(@error, @extended, 0) If BitAND($iFlag, 1) Then Return SetError($lastError[0], $lastError[0], 0) Else Exit -1 EndIf EndIf Return $handle[0] EndFunc ;==>_SingletonEX My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Musashi Posted February 19, 2021 Share Posted February 19, 2021 2 hours ago, water said: Sleep(@AutoItPID * 10) Hi @water , the PID varies, of course, but is often in the range of 500 to 5000 . If you multiply that by 10, you get sleep values from 5 to 50 seconds. Silly question, but isn't this a bit time consuming ? "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Link to comment Share on other sites More sharing options...
water Posted February 19, 2021 Share Posted February 19, 2021 This is an automated job running during nighttime (onboarding process). So time isn't an issue Musashi 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Burgaud Posted April 25, 2021 Author Share Posted April 25, 2021 #include <Misc.au3> #include <MsgBoxConstants.au3> for $i = 1 to 10 MsgBox($MB_SYSTEMMODAL, $i , _Singleton("_Singleton", 1), 3) next I was having weird results using _Singleton so I went back and tested it once again. This time, I ran the above script twice, 2nd script about a few seconds after the 1st (but the 1st script is still running). The 1st script run as usual, displaying a non-zero (Handle) at first then displaying 0s thereafter (which is as expected). The 2nd script however initially displays 0 (which is as expected), then sometime later (3rd or 4th?) displays a non-zero (once), then went back displaying 0s again. Why did the 2nd script get a non-zero? Wasn't it suppose to always get a zero while the 1st script is still running? Link to comment Share on other sites More sharing options...
Nine Posted April 25, 2021 Share Posted April 25, 2021 17 hours ago, Burgaud said: Wasn't it suppose to always get a zero while the 1st script is still running? I tested it a few times (with different lags between the 2 scripts) and it works as expected. When 1st script ends, 2nd can capture the mutex. As long as the 1st script runs, 2nd only shows 0 as result. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy 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