RJP Computing Posted December 2, 2004 Share Posted December 2, 2004 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/ Link to comment Share on other sites More sharing options...
Chris_1013 Posted December 2, 2004 Share Posted December 2, 2004 Check the FAQ in the documentation, or online here http://www.autoitscript.com/autoit3/docs/faq.htm#14There is a simple suggestion offered, which is14. 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 runningAutoItWinSetTitle($g_szVersion); Rest of your script goes here Link to comment Share on other sites More sharing options...
SlimShady Posted December 2, 2004 Share Posted December 2, 2004 Here's my version. But I still haven't used it in my scripts. If WinExists(@ScriptName) Then Exit AutoItWinSetTitle(@ScriptName) Link to comment Share on other sites More sharing options...
Valik Posted December 2, 2004 Share Posted December 2, 2004 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 More sharing options...
SlimShady Posted December 2, 2004 Share Posted December 2, 2004 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. Link to comment Share on other sites More sharing options...
Nova Posted December 2, 2004 Share Posted December 2, 2004 (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 ;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 December 2, 2004 by nova Link to comment Share on other sites More sharing options...
Helge Posted December 2, 2004 Share Posted December 2, 2004 (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 December 2, 2004 by Helge Link to comment Share on other sites More sharing options...
Somerset Posted December 2, 2004 Share Posted December 2, 2004 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). Link to comment Share on other sites More sharing options...
scriptkitty Posted December 2, 2004 Share Posted December 2, 2004 (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 December 2, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
Developers Jos Posted December 2, 2004 Developers Share Posted December 2, 2004 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 More sharing options...
ezzetabi Posted December 2, 2004 Share Posted December 2, 2004 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. Link to comment Share on other sites More sharing options...
scriptkitty Posted December 2, 2004 Share Posted December 2, 2004 (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 December 2, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
Developers Jos Posted December 2, 2004 Developers Share Posted December 2, 2004 Looks to me that the solution, as posted by Chris_1013, is simple and effective. 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 More sharing options...
Valik Posted December 2, 2004 Share Posted December 2, 2004 @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. Link to comment Share on other sites More sharing options...
RJP Computing Posted December 3, 2004 Author Share Posted December 3, 2004 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/ Link to comment Share on other sites More sharing options...
scriptkitty Posted December 3, 2004 Share Posted December 3, 2004 (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 December 3, 2004 by scriptkitty AutoIt3, the MACGYVER Pocket Knife for computers. Link to comment Share on other sites More sharing options...
ezzetabi Posted December 4, 2004 Share Posted December 4, 2004 I would say:- Really amazing that you had NO ideas. It is really amazing how everybody has a different idea. Thanks.<{POST_SNAPBACK}> Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now