Jump to content

New approach to only allowing one instance


Valik
 Share

Recommended Posts

For the commented line it was a modification I did to Valik code when I proposed to include it iin the standard "Misc.au3". :P

Speaking of, why did you change that line, anyway? For all intents and purposes, a Semaphore is just as good as a Mutex in this case.
Link to comment
Share on other sites

Ahhh so there is debate :P

No there isn't. JP changed the code for reasons I don't know and for reasons I don't care about from a technical perspective because in this usage case a Mutex serves an identical purpose to a Semaphore. I'm merely curious as to why the code was changed. I don't have a problem with using a Mutex nor do I have any compelling reason to use a Semaphore other than that is my personal preference.
Link to comment
Share on other sites

  • 1 month later...

The CreateSemaphore function is available in ALL versions of Windows, since Windows 95, as documented by this MSDN article:

http://msdn.microsoft.com/library/default....tesemaphore.asp

Requirements:

  • Client Requires Windows Vista, Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
  • Server Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server.
  • Header Declared in Winbase.h; include Windows.h.
  • Library Link to Kernel32.lib.
  • DLL Requires Kernel32.dll.
  • Unicode Implemented as CreateSemaphoreW (Unicode) and CreateSemaphoreA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.

:lmao:

[right][img]style_emoticons/autoit/robot.gif[/img]One of TenSecondary Adjunct of Unimatrix Z03[/right]

Link to comment
Share on other sites

Thanks to DllCall(), the proper way of only allowing a single instance of a script to run is now possible. Here is the code:

Func Singleton($semaphore)
    Local $ERROR_ALREADY_EXISTS = 183
    DllCall("kernel32.dll", "int", "CreateSemaphore", "int", 0, "long", 1, "long", 1, "str", $semaphore)
    Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    If $lastError[0] = $ERROR_ALREADY_EXISTS Then Exit -1
EndFunc; Singleton()

Once a semaphore is created, a second attemp to create it will result in ERROR_ALREADY_EXISTS. By checking for that error, you know that an instance is already running.

The main advantage to this over changing the window title is that its a lot harder to spoof or accidentally trigger since the semaphore is more likely to be completely unique than a window title. It also means the internal window title can be used for whatever purpose the scripter desires.

The parameter this function takes should be unique, but not generated randomly. It must be the same between all versions of the script, but it must be different from any other scripts. The longer and more random it is, the less likely to conflict with anything else (But again, it can't be randomly generated at run-time or it will defeat the purpose).

I am looking for some guidance here. I have multiple applications that all update a single file. Could I use the Semiphore to control acces to this file? I assume I would have to impliment OpenSemaphoe, as well as ReleaseSemaphore? Do you see this as a legitimate use of the Semaphore? I have used this function to ensure one instance of a running application, and it works great.

Talking Clockhttp://www.autoitscript.com/forum/index.php?showtopic=20751Talking Headlineshttp://www.autoitscript.com/forum/index.php?showtopic=20655Sometimes, I sits and thinkssometimes, I just sits

Link to comment
Share on other sites

Synchronizing access to shared resources is the exact reason for the existence of the synchronization objects (Which includes a Semaphore). There's plenty of documentation on MSDN for how to use those objects for synchronizing access to a resource.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...