#1 27 Dec 2012 10:50

Ed Dyreen
Member
From: Flanders_( Belgium )
Registered: 29 Feb 2008
Posts: 18
Website

mutex, singleTon

'
I'm about to implement following functions:

' Set oMutex = mutexUnique_( lsMutex ) 	' Returns a unique file mutex.
' Set oMutex = mutexOpen_( lsMutex ) 	' Attempts to open a file mutex.
' Set oMutex = mutexClose_( loMutex ) 	' Closes a file mutex.
' succes = singleTon_( sMutex, wait )

I can probably do this playing with fileopen for writing and checking the result. But I strongly doubt this is the "correct" way of creating a mutex. In autoIT I would do something like this:

$handle = DllCall( 'kernel32.dll', 'handle', 'CreateMutexW', 'struct*', $tSecurityAttributes, 'bool', 1, 'wstr', $sOccurenceName )

Thanks,

Last edited by Ed Dyreen (27 Dec 2012 15:57)

Offline

#2 27 Dec 2012 12:39

Ed Dyreen
Member
From: Flanders_( Belgium )
Registered: 29 Feb 2008
Posts: 18
Website

Re: mutex, singleTon

'
Ok so this is the file based mutex I mentioned earlier.

smutex 	= ""
i 	= 0
iTime 	= 1
'
Do Until IsObject( oFileMutexChild )
	'
	On Error Resume Next
	Set oFileMutex = oFSO.OpenTextFile( "C:\ED\mutex\mutex", iForAppending, True )
	getErr_( Err.Number )
	On Error Goto 0
	'
	If error = 0 Then
		'
		i = i + 1
		On Error Resume Next
		Set oFileMutexChild = oFSO.OpenTextFile( "C:\ED\mutex\mutex" &i, iForAppending, True )
		getErr_( Err.Number )
		On Error Goto 0
		oFileMutex.Close
		'
	End If
	'
	If error <> 0 Then iTime = sleep_( iTime )
	'
Loop
oStdOut.write( vbNewLine &" 'C:\ED\mutex\mutex" &i &"'" )

But I don't like it, I don't like having to write a file just to create a mutex.

sad

Last edited by Ed Dyreen (27 Dec 2012 14:33)

Offline

#3 31 Mar 2015 12:50

zoodeef
New Member
Registered: 31 Mar 2015
Posts: 1

Re: mutex, singleTon

It's a totally valid approach, but once the singleton is created, there isn't really any ... std::atomic<Singleton*> Singleton::m_instance; std::mutex ...


adfd

Offline

#4 22 Jun 2015 15:27

AikonCWD
New Member
Registered: 22 Jun 2015
Posts: 1

Re: mutex, singleTon

Use this code

''Objects
set shell = createObject("wscript.shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")


''Check the args passed into the script. Specifically looking for the last argument which is the script mutex
Set args = Wscript.Arguments
argsFound = false
For Each arg In args
	argsFound = arg
Next

''If we didn't get any arguments re-run self with a random arg string, this will be the mutex
if argsFound = false then
	runSelf(genRndStr(8))
Else

	''The last argument is the mutex string
	mutex = argsFound
end if 

''remove any other instances of this script
killPastInstances(mutex)



''This sub will kill all instnances of the currently running vbscript that are running under the same interpreter
''but it will not kill it's self
''note, this requires that this script has a uniquite mutex
sub killPastInstances(mutex)

	''Get self name
	scriptName = WScript.ScriptFullName

	Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process",,48) 
	For Each objItem in colItems 

		if instr(objItem.CommandLine, scriptName) > 0 Then
		  ''If the instance of the script is NOT this instance
		  if not instr(objItem.CommandLine, mutex) > 0 then

		  	''Kill it!
		  	objItem.Terminate()
		  end if
		end if
	Next
end sub


''generates a random string of length "count"
Function genRndStr(Count)
    Randomize
    For i = 1 To Count
        If (Int((1 - 0 + 1) * Rnd + 0)) Then
            genRndStr = genRndStr & Chr(Int((90 - 65 + 1) * Rnd + 65))
        Else
            genRndStr = genRndStr & Chr(Int((57 - 48 + 1) * Rnd + 48))
        End If
    Next
End Function


''re-runs the curernt script with args in cscript if it is running in wscript. current script exits
sub forceCscript
  If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then

	Set args = Wscript.Arguments
	argStr = ""
	For Each arg In args
		argStr = argStr & " " & arg
	Next

    Shell.Run "cscript """ & WScript.ScriptFullName & """" & argStr, 1, False
    WScript.Quit
  End If
end sub



''Runs a new instance of the current script with additional arguments. Current script exits
sub runSelf(extraArgStr)
  
	''Are we runing in C or W script?
	interpreter = "wscript.exe"
	If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
		interpreter = "wscript.exe"
	else
		interpreter = "cscript.exe"
	end if

	''Get current args
	Set args = Wscript.Arguments
	argStr = ""
	For Each arg In args
		argStr = argStr & " " & arg
	Next

	''append new args if required
	if extraArgStr <> "" then argStr = argStr & " " & extraArgStr

    Shell.Run interpreter & " """ & WScript.ScriptFullName & """" & argStr, 1, False
    WScript.Quit
  
end sub

Credits to: kellestine.com/vbscript-mutex/

Offline

Board footer

Powered by FluxBB