Jump to content

how to have only one instance of a script running


oholdeno
 Share

Recommended Posts

i've compiled a script into an executable. it's working great. the only thing i'd like to fix is to somehow have it only run once instance of it at any time. running multiple instances will mess up how the program works. can anyone help me out?

one of the top questions in the FAQ's

14. How can I make sure only one copy of my script is run?

The easiest way is to rename the title of the hidden AutoIt window when your script first starts. Then in the same script check for that window title existing - if it does then another copy of the script is running.

; 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

Back To Top

8)

NEWHeader1.png

Link to comment
Share on other sites

i've compiled a script into an executable. it's working great. the only thing i'd like to fix is to somehow have it only run once instance of it at any time. running multiple instances will mess up how the program works. can anyone help me out?

see _Singleton UDF functions ;)
Link to comment
Share on other sites

I used this line right after all my #include

If UBound(ProcessList(@ScriptName)) > 2 Then Exit

It only allow one instance of a script running....

No, all that does is disallows one named instance of the same copy of a script to be running. If I copy/rename your script while it's already running, I can start a second instance. Relying on something dynamic for your singleton pattern enforcement is a recipe for disaster because dynamic things can change (Funny how that works, no?).
Link to comment
Share on other sites

  • Moderators

This gives 2 other alternatives of what the masses of the group here have put, but more importantly it gives the correct Semaphore that Valik is talking about.

The first example is Valiks correct one, the 2nd is my attempt to make sure it got the correct file.

Semaphore vs. File Search

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

This gives 2 other alternatives of what the masses of the group here have put, but more importantly it gives the correct Semaphore that Valik is talking about.

The first example is Valiks correct one, the 2nd is my attempt to make sure it got the correct file.

Semaphore vs. File Search

Your second example is inherently flawed as well. What happens if for some reason I have a binary already running with the same name as your script but it's not your script? In that case, I won't be able to start any instance.

Any time you try to base a singleton pattern around some dynamic object, it is no longer a singleton. The checked object has to be immutable, otherwise, it will be possible to circumvent the singleton checks and get two instances.

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