Jump to content
Sign in to follow this  
cypher175

_Singleton() Usage..?

Recommended Posts

cypher175

is there anyway to use the _Singleton() Function without specifying the exact process name in between the ("Brackets")

So no matter what the script.exe is named only 1 Instance of it can run at a time..??

Share this post


Link to post
Share on other sites
dbzfanatic

Share this post


Link to post
Share on other sites
cypher175

If it's your script you can use

If _Singleton(@scriptname) = 0 Then
MsgBox(0,"Error","The script is already running.")
Exit
Endif
thank you..

Share this post


Link to post
Share on other sites
PsaltyDS

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

Share this post


Link to post
Share on other sites
cypher175

@ 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..??

Share this post


Link to post
Share on other sites
rasim

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:

Share this post


Link to post
Share on other sites
trancexx

@ 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

Share this post


Link to post
Share on other sites
PsaltyDS

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

Share this post


Link to post
Share on other sites
trancexx

I would like to express strong disagreement with some parts of the last post here (prior this one).

I had few laughs (that person related) here lately.


♡♡♡

.

eMyvnE

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.