Jump to content
Sign in to follow this  
RJP Computing

How do you make a script only run once?

Recommended Posts

RJP Computing

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/

Share this post


Link to post
Share on other sites
Chris_1013

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

Share this post


Link to post
Share on other sites
SlimShady

Here's my version. But I still haven't used it in my scripts.

If WinExists(@ScriptName) Then Exit
AutoItWinSetTitle(@ScriptName)

Share this post


Link to post
Share on other sites
Valik

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

Share this post


Link to post
Share on other sites
SlimShady

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.

Share this post


Link to post
Share on other sites
Nova

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

Share this post


Link to post
Share on other sites
Helge

@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

Share this post


Link to post
Share on other sites
Somerset

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


 

Spoiler

shoot_zpsfd329d66.png
dontbelieveeverythingyouthink_zps0e1e900

Madness is the first step to understanding...

Share this post


Link to post
Share on other sites
scriptkitty

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.

Share this post


Link to post
Share on other sites
Jos

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 Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
ezzetabi

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.

Share this post


Link to post
Share on other sites
scriptkitty

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.

Share this post


Link to post
Share on other sites
Valik

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

Share this post


Link to post
Share on other sites
RJP Computing

Thanks for all the ideas. I ended up going with the semephore solution. Clean, simple, and effective. Cool ideas though. It is really amazing how everybody has a different idea. Thanks.


-RyanRJP Computinghttp://rjpcomputing.com/

Share this post


Link to post
Share on other sites
scriptkitty

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.

Share this post


Link to post
Share on other sites
ezzetabi

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

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  

×