Jump to content

_Singleton() Usage..?


Recommended Posts

Link to comment
Share on other sites

If it's your script you can use

If _Singleton(@scriptname) = 0 Then
MsgBox(0,"Error","The script is already running.")
Exit
Endif
The macro @ScriptName will be interpreted at compile time. If you compile again with a different script name (i.e. MyScript_v1.au3 and MyScript_v2.au3) then it will not recognize an instance of the other version.

The string you provide _Singleton() can be as much as you need to uniquely identify the script:

If _Singleton("MyTestScriptWhichMustOnlyHaveOneRunningInstance_0123_4567_89AB_CDEF") = 0 Then
MsgBox(0,"Error","The script is already running.")
Exit
Endif

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

cypher175

Singleton using Mutex object:

Dim $hMutex = DllCall("kernel32.dll", "hwnd", "OpenMutex", "int", 0x1F0001, "int", False, "str", "MyProgramUniqueName")

If $hMutex[0] Then
    MsgBox(64, "MyProgram", "Program is already running")
    Exit
EndIf

DllCall("kernel32.dll", "hwnd", "CreateMutex", "int", 0, "int", False, "str", "MyProgramUniqueName")

:mellow:

Link to comment
Share on other sites

@ PsaltyDS

I not really sure what you are concluding..??

Are you say that all i rally need to do is just use _Singleton() just like that with no name inside the brackets or what exactly are you saying to do..??

May I?

He's saying that you shouldn't use @ScriptName because that can be changed (by you or whoever working with you on some project when making new versions of it, for example).

Use "solid" string for that purposes. I would suggest generating GUID, but I guess "MyTestScriptWhichMustOnlyHaveOneRunningInstance_0123_4567_89AB_CDEF" is fine too.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

cypher175

Singleton using Mutex object:

Dim $hMutex = DllCall("kernel32.dll", "hwnd", "OpenMutex", "int", 0x1F0001, "int", False, "str", "MyProgramUniqueName")

If $hMutex[0] Then
    MsgBox(64, "MyProgram", "Program is already running")
    Exit
EndIf

DllCall("kernel32.dll", "hwnd", "CreateMutex", "int", 0, "int", False, "str", "MyProgramUniqueName")

:(

That's how _Singleton() already works. Check the code for it inside the Misc.au3 UDF. :)

May I?

He's saying that you shouldn't use @ScriptName because that can be changed (by you or whoever working with you on some project when making new versions of it, for example).

Use "solid" string for that purposes. I would suggest generating GUID, but I guess "MyTestScriptWhichMustOnlyHaveOneRunningInstance_0123_4567_89AB_CDEF" is fine too.

Exactly right. The string I provided was meant to be a little over the top and therefore mildly humorous, but nobody ever seems to find the same things funny that I do... :mellow:
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...