Sign in to follow this  
Followers 0
Kurtferro

_Singleton

8 posts in this topic

Q3. How can I prevent more than one copy of my script from running at once / detect another copy of my script running?

If you simply want know if another copy is running, you can use code similar to the next example:

CODE: AutoIt

#include <Misc.au3>

If _Singleton("MyScriptName", 1) Then

; We know the script is already running. Let the user know.

MsgBox(0, "Script Name", "This script is already running. Using multiple copies of this script at the same breaks the [(UltimaCoder)] License!")

Exit

Endif

I have tried it but for me dont work.

I need a singol istance.

My script name is "ServerPorter"

Share this post


Link to post
Share on other sites



#Include <Misc.au3>
_Singleton("ServerPorter", 0)

Putting this at the top of the script will detect when opened if another copy is open and Exit upon finding it.

Share this post


Link to post
Share on other sites

I have already tried in all mode, but it never work with me, can be a problem with vista?

i have made this function:

Func CheckPID()

$list = ProcessList("ServerPorter.exe")
for $i = 1 to $list[0][0]
    if  $list[$i][1] <> @AutoItPID then
        msgbox(4096, $list[$i][0], "ServerPorter.exe is already executed!!")
        Sleep(5000)
        exit 0
    EndIf
next

EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This is another example of how to do that from the help file (FAQ section)

; Place at the top of your script

$g_szVersion = "My Script 1.1"

If WinExists($g_szVersion) Then Exit ; It's already running

AutoItWinSetTitle($g_szVersion)

; Rest of your script goes here

Try it out

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

K, it a solution, mine work too, but the problem is why _Singleton dont work? is my problem or is a bug? someone can try it and report the OS used?

Share this post


Link to post
Share on other sites

I use _Singleton on a few scripts with no problems, including _Singleton(@scriptname, 1). I am runing XP SP2.

Share this post


Link to post
Share on other sites

I have already tried in all mode, but it never work with me, can be a problem with vista?

i have made this function:

Func CheckPID()

$list = ProcessList("ServerPorter.exe")
for $i = 1 to $list[0][0]
    if  $list[$i][1] <> @AutoItPID then
        msgbox(4096, $list[$i][0], "ServerPorter.exe is already executed!!")
        Sleep(5000)
        exit 0
    EndIf
next

EndFunc
If your running Vista it doesn't hurt to put #RequireAdmin at the top of your code. Whenever I am making scripts that will be ran on a Vista system I just automatically put it in, it has made some work that should have otherwise been fine but wouldn't work.

Vista's security is a hassle for admins as well as many other things.

#RequireAdmin

Share this post


Link to post
Share on other sites

i will try, but my UAC is disabled.

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  
Followers 0