water Posted March 27, 2012 Share Posted March 27, 2012 mutex afaik My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
slbmeh Posted March 28, 2012 Share Posted March 28, 2012 slbmeh,I believe _singleton uses either semaphores or a mutex.kylomasI wasn't familiar with the functionality of _Singleton until now, the name threw me off. I assumed that it would either only allow one instance of the application or queue execution of an application until the previous had completed. The documentation says Mutex.The purpose of a mutex is to restrict the same piece of code or data from being used at the same time, whereas a binary semaphore would be more applicable in this situation because it ensures singular access to a specific resource. In a threaded application, or multiple instances of an application running parallel one would assume that the task can be executed on multiple resources simultaneously. Usage of a Mutex would limit execution of a function until the previous process has completed its task. A binary semaphore would instead allow for an application to execute the segment of code as long as the resource it is attempting to access were a different resource. Link to comment Share on other sites More sharing options...
kylomas Posted March 28, 2012 Share Posted March 28, 2012 slbmeh, Yes, and thus the name "_singleton". This issue comes up frequently, especially with regard to serializing access to data. There are several disparate commonly suggested solutions. It would be to everyones benefit to develop a paradigm (a Valik attempted with _singleton) that provides the granularity that you suggest for "read", "write", "alter", and "delete" access. Unfortunately, I do not have the skills on this platform to do this, perhaps you do! kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
EdDyreen Posted April 4, 2012 Author Share Posted April 4, 2012 (edited) Hi Ed, here is the code I use to make sure that scripts started in a bulk run serialized. It's a modified and stripped down version of _Singleton. you can find the whole discussion.expandcollapse popup;--------------------------------------------------------------------- ; Make sure that only one script runs at a time ;--------------------------------------------------------------------- While 1 If _SingletonEx("New Employee", 1) = 0 Then Sleep(2000) Else ExitLoop Endif WEnd ; #FUNCTION# ==================================================================================================================== ; Name...........: _SingletonEx ; Description ...: Enforce a design paradigm where only one instance of the script may be running. ; Syntax.........: _SingletonEx($sOccurenceName[, $iFlag = 0]) ; Parameters ....: $sOccurenceName - String to identify the occurrence of the script. This string may not contain the character unless you are placing the object in a namespace (See Remarks). ; $iFlag - Behavior options. ; |0 - Exit the script with the exit code -1 if another instance already exists. ; |1 - Return from the function without exiting the script. ; |2 - Allow the object to be accessed by anybody in the system. This is useful if specifying a "Global" object in a multi-user environment. ; Return values .: Success - The handle to the object used for synchronization (a mutex). ; Failure - 0 ; Author ........: Valik ; Modified.......: water based on code modified by smartee ; Remarks .......: You can place the object in a namespace by prefixing your object name with either "Global" or "Local". "Global" objects combined with the flag 2 are useful in multi-user environments. ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== 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 ' Water, I implemented your adaptation of _singleton, _singletonEx and wrote 2 functions around it to fit my needs, expandcollapse popup; ---------------------------------------------------------------------------------------------------------------------------------- Func FileOpen_( $process = 'fileLock' ) ; ---------------------------------------------------------------------------------------------------------------------------------- ; last updated : 04/04/2012 ; ; Serialize any process ; ;Debug_On_( $func ) ; ( ;Debug_( 'valid ' &$func &'( ' &$args &' )' ) ; ( Local $handle, $time ; ) ;Debug_( 'perform ' &$func &'( ' &$args &' )', 'pause' ) ; ( $handle = 0 $time = 0 ; While $handle = 0 $time = Sleep_( $time ) $handle = SingletonEx_( $process, 1 ) WEnd ; Assign( $process, $handle , 2 ) ; ) ; EndFunc ; ) ; ---------------------------------------------------------------------------------------------------------------------------------- #cs Debug_On_ ( @ScriptName ) MsgBox ( 0, '', 'begintest' ) callS_ ( 'FileOpen_' ) MsgBox ( 0, '', 'endoftest' ) #ce ; ---------------------------------------------------------------------------------------------------------------------------------- Func FileClose_( $process = 'fileLock' ) ; ---------------------------------------------------------------------------------------------------------------------------------- ; last updated : 04/04/2012 ; ; Normalize any process ; ;Debug_On_( $func ) ; ( ;Debug_( 'valid ' &$func &'( ' &$args &' )' ) ; ( ; ) ;Debug_( 'perform ' &$func &'( ' &$args &' )', 'pause' ) ; ( DllCall( 'kernel32.dll', 'bool', 'CloseHandle', 'handle', Eval( String( $process ) ) ) ; ) ; EndFunc ; ) ; ---------------------------------------------------------------------------------------------------------------------------------- #cs Debug_On_ ( @ScriptName ) MsgBox ( 0, '', 'begintest' ) callS_ ( 'FileClose_' ) MsgBox ( 0, '', 'endoftest' ) #ce I hope I'm using it correctly ? FileOpen_() MsgBox( 0, '', 'Serial' ) FileClose_() MsgBox( 0, '', 'Parralel' ) exit I haven't tested it thoroughly but it seems to work perfectly, Thanks for helping me devellop this game bot ! Edited April 6, 2012 by EdDyreen • Any number images • Images of any size • Any number of URLs • Any number of lines Link to comment Share on other sites More sharing options...
water Posted April 4, 2012 Share Posted April 4, 2012 Looks good! My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki 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