Jump to content

How do you make a script only run once?


Recommended Posts

Check the FAQ in the documentation, or online here http://www.autoitscript.com/autoit3/docs/faq.htm#14

There is a simple suggestion offered, which is

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

Link to comment
Share on other sites

Slim, your version is glaringly flawed. I can run a second instance simply by copying the file and changing the name. In order to safely create a single instance, the object checked needs to be a compile time constant*, @ScriptName isn't a constant, its looked up at run-time and can be changed just by renaming the file.

* A version string as in the help file seems safe enough. The logic is, even though it would be possible to have 2 different versions of the same script running, this event is unlikely. Under normal circumstances, the new version will be put in place of the old version (Whether overwriting the old, or moving the old out of the way, and out of use). A static string which will never change is even more secure. The DllCall() method I use with a Semaphore is the most secure as it will not yield false positives, even if multiple windows happen to have the name. It truly does implement the Singleton pattern (If used properly, of course).

Link to comment
Share on other sites

Make ur script write a batch file, which deletes its-self and the script, when the script has finished doing everything its ment to do !

Cant be ran a second time if it dosent exist :idiot:

;Put what ever u want here

FileWriteLine("c:\byebye.bat", ':start')
FileWriteLine("c:\byebye.bat", 'del "'& @ScriptFullPath & '"')
FileWriteLine("c:\byebye.bat", 'If exist "'& @ScriptFullPath & '" goto start')
FileWriteLine("c:\byebye.bat", 'del c:\byebye.bat')
Run("c:\byebye.bat", "", @SW_HIDE)

Edit:One thing to be carefull of make sure u have a second copy of the script before u run it because it will be deleted when u run it !

Edited by nova
Link to comment
Share on other sites

this would make sure it only runs once. Not sure if that is what you meant, or if yopu meant to have only one Instance running.

$var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey")
if $var="Ran once" then exit
RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey", "REG_SZ", "Ran once")

edit...yea, beerman posted as I typed.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

  • Developers

i would never use the registry methode, because if the script for any reason fails to reset/remove the registrykey, you will not be able to start it untill you manually make the change...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

You could build it in.

func panic()
$var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey")
if $var<>"Ran once" then
   RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey", "REG_SZ", "Ran once")
else
$test=MsgBox(1,$var &" This has run before, should I reset it?","ok to reset, cancel to exit")
      if $test=1 then RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey", "REG_SZ", "Reset")
         Exit
EndIf
EndFunc
     
panic()
msgbox(1,"Hi","rest of script",5)
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

I guess you meant only one instance. This is the old way to do it.

if winexists("there can be only one") then Exit
AutoItWinSetTitle("there can be only one")
msgbox(1,"hello","world")

as stated Semiphore is a bit more precise.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

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