Jump to content

Recommended Posts

Posted

How can I make it so that a compiled script can only be ran once?

I need this because the script runs all the time and if I don't have that I don't get results as expected.

I hope this question isn't to stupid. :"> I am sure that I am missing something simple.

Thanks.

-RyanRJP Computinghttp://rjpcomputing.com/

Posted

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

Posted

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

Posted

I know it's flawed.

I don't have different scripts with the same name. That's why I would use it.

It's just a suggestion. Ofcourse there are better ways.

Posted (edited)

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
Posted (edited)

@Valik : Just a quick question... How does that Semaphore work ?

I`ve seen you talked about it before, but I`m unable to test it here..

Thanks..

Edited by Helge
Posted

another simple way to make sure a run once is achieved. have autoit write something into the registry something simple, and have the script to see if it is there. if it had been ran before (if it exist, then don't run).

Posted (edited)

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.

  • Developers
Posted

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

Posted

You should write down not a single key but actually the started PID.

So IF the key exist and the PID exist then don't start. Otherwise delete the key (if exists of course) and starts.

Posted (edited)

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.

Posted

@Valik : Just a quick question... How does that Semaphore work ?

I`ve seen you talked about it before, but I`m unable to test it here..

Thanks..

<{POST_SNAPBACK}>

See here.
Posted (edited)

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.

Posted

I would say:- Really amazing that you had NO ideas. :idiot::D

It is really amazing how everybody has a different idea. Thanks.

<{POST_SNAPBACK}>

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...