soulhealer Posted July 21, 2006 Share Posted July 21, 2006 how to prevent a compiled script, to be ran multiple times in a time? i mean, prevent it to be opened as more than one process. Link to comment Share on other sites More sharing options...
MHz Posted July 21, 2006 Share Posted July 21, 2006 From memory, FAQ Number 14 Link to comment Share on other sites More sharing options...
Squirrely1 Posted July 21, 2006 Share Posted July 21, 2006 (edited) You must mean that you want to ensure that only one instance of your program is allowed to run at a given moment. Have your script examine the running processes another instance by name using ProcessList() and exiting if it is there. This code belongs near the top of your script before you create any windows, and soon before much is done besides initializing variables and such. Global $list, $i $list = ProcessList(@ScriptName) For $i = 1 To $list[0][0] ; Next If $i > 2 Then #cs MsgBox($MB_OK+$MB_ICONEXCLAMATION+$MB_TOPMOST, _ $Software_Title & "- Setup Error", _ "There is already an instance of this program. " & @CRLF & _ "Only one is allowed. " & @CRLF & _ "Ending execution.") #ce Exit EndIf With this code in place, two instances might run for a short time, but the second one will end execution very soon after it starts. Edited July 21, 2006 by Squirrely1 Das Häschen benutzt Radar Link to comment Share on other sites More sharing options...
/dev/null Posted July 21, 2006 Share Posted July 21, 2006 From memory, FAQ Number 14 Absolutely correct, however there is a better way in the beta: _Singleton().CheersKurt __________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf * Link to comment Share on other sites More sharing options...
Valik Posted July 22, 2006 Share Posted July 22, 2006 You must mean that you want to ensure that only one instance of your program is allowed to run at a given moment. Have your script examine the running processes another instance by name using ProcessList() and exiting if it is there. This code belongs near the top of your script before you create any windows, and soon before much is done besides initializing variables and such. Global $list, $i $list = ProcessList(@ScriptName) For $i = 1 To $list[0][0] ; Next If $i > 2 Then #cs MsgBox($MB_OK+$MB_ICONEXCLAMATION+$MB_TOPMOST, _ $Software_Title & "- Setup Error", _ "There is already an instance of this program. " & @CRLF & _ "Only one is allowed. " & @CRLF & _ "Ending execution.") #ce Exit EndIf With this code in place, two instances might run for a short time, but the second one will end execution very soon after it starts.You should be shot repeatedly for even suggesting this method. It's so full of holes that any halfwit can accidentally bypass it. If you want to know why it's the single worst method for doing what the OP asked for, search for other posts by me on the subject why I explain multiple ways your method will fail. Link to comment Share on other sites More sharing options...
Paulie Posted July 22, 2006 Share Posted July 22, 2006 (edited) You should be shot repeatedly for even suggesting this method. It's so full of holes that any halfwit can accidentally bypass it. If you want to know why it's the single worst method for doing what the OP asked for, search for other posts by me on the subject why I explain multiple ways your method will fail. Ouch...@squirrelyJust do what Valik says and reply to this saying he is totally right and you'll never suggest a method like this againEDIT:Or don't reply at all Edited July 22, 2006 by Paulie Link to comment Share on other sites More sharing options...
Squirrely1 Posted July 23, 2006 Share Posted July 23, 2006 (edited) To soulhealer: MHz told you correctly in the first place, because I still haven't read FAQ #14. The last time I read that page was in the production version where there were only 10 FAQs. If you use the production version of AutoIt, then use this: ; For use with the production version of AutoIt (basic method suggested by helpfile FAQ #14) ; Place this near the top of your script $g_szVersion = "My Somewhat Unique Window Title" If WinExists($g_szVersion) Then ; An instance is already running! Exit EndIf AutoItWinSetTitle($g_szVersion) or this: ;Method that looks like it would work with the production version of AutoIt: If UBound(ProcessList(@ScriptName)) > 2 Then ;MsgBox(262208, "* NOTE * ", "Welcome to AutoIt 1-2-3 was already running ", 5) Exit EndIf If you use the beta version of AutoIt, get the benefit of Valik's hard work, and use this: ; For use with the latest AutoIt beta, put this near the beginning of your script: #include <Misc.au3> If Singleton("[InsertHereAnyUniqueString; ItServesAsAnInstanceIdentifier (or semaphore)]", 1) == 0 Then ; An instance is already running! Exit EndIf Forum Reference To whom it may concern... Quotes of Valik on the subject: "You should be shot repeatedly for even suggesting this method." "...I don't even use the version in Misc.au3 which offers more functionality than my original. I just continue to use the original function posted in the thread I created. " "Calling trivial code complex and saying it is convoluted when it is the standard method for doing something shows a very high level of ignorance..." "...does it seem wise to question a developer's code about stability..." "...perhaps if you would [have]just asked how the function [works] instead of polluting the posts with [ignorant] comments on the subject...perhaps it wouldn't be so bad.... In addition, even if still compelled to ask, the extraneous comments were unnecessary and served only to make the important information more lost in the sea of words." "And for the record, using a Semaphore/Mutex is the correct way under Windows programming for allowing only a single instance of an application to run."Famous Valik quotes: "In the future, may I suggest you be careful about who's code you question." "...it is an error [for you to communicate]your needs correctly." "I wasn't even being hard on you and you're whining about how you're being treated." "It's too bad I do know what I'm talking about, then, which makes you dead wrong."Famous Responses to Valik: "I don't care what or who you are, or what little thing it is that you do -- stay away from public interchanges until you can conduct yourself in a decent, normal, civilized manner." New Response to Valik: I am put back in my place, sir. While you are the elder aboard, I am merely the jetsam. My method could indeed fail, but I can see only one way for it to fail when the program is run locally, and that is if the user changes the name of the executable. Oh, but I am yet the flotsam--don't get me wrong. I seek your higher wisdom, for, as you can obviously see, I am but a 7-year-old black-and-white starlet left over from the late 1950s, and I felt truly compelled to follow your every instruction up to this point, including being shot repeatedly. And I am glad you had me search the forums, because not only did I end up finding the correct syntax for _Singleton() so I could use that command, I also in the process, happened upon the method for deleting the currently running executable file. Thanks Valik, and pardon me, MHz: mea culpa executed this Sunday morning, the twenty-third day of July, in the year of Our Lord Jesus Christ twenty-hundreds and six. Edited July 23, 2006 by Squirrely1 Starstar 1 Das Häschen benutzt Radar Link to comment Share on other sites More sharing options...
Valik Posted July 23, 2006 Share Posted July 23, 2006 I am put back in my place, sir. While you are the elder aboard, I am merely the jetsam. My method could indeed fail, but I can see only one way for it to fail when the program is run locally, and that is if the user changes the name of the executable.Which is why I say any halfwit can do it. Anybody can rename a process. It doesn't require deep Windows knowledge or programming experience. Anybody can intentionally or accidentally rename a process, even while it is running, and then run it again.Your script fails completely if the script is run non-compiled. @ScriptName is not a valid process name when running non-compiled. In that case, the actual name of the process will be "AutoIt3.exe".Your script will fail if you use /AutoIt3ExecuteScript and the script you are executing uses the process method of determining it is only instance running. The script being executed will be the second process to be created and thus will never run.It is not safe to rely on the process name. It works in some situations. It lulls you into thinking it works in all situations and then when it fails you will wonder why. The two methods I have provided (Window title or _Singleton()) will not fail unless somebody is determined to make it fail. With the window-title method, the programmer may accidentally change the title so the _Singleton() method is recommended. Unless somebody knows how to enumerate and close a handle in a separate process, the code will work. Link to comment Share on other sites More sharing options...
Starstar Posted November 14, 2013 Share Posted November 14, 2013 Thanks a lot of you Squirrely1........ Life is like a coin. You can spend it Anyway as you wish and for your kind information. "you can spend it only once." Link to comment Share on other sites More sharing options...
Developers Jos Posted November 14, 2013 Developers Share Posted November 14, 2013 (edited) Thanks a lot of you Squirrely1........ .. and you for resurrecting a 7+ years old post to share your gratitude my dear adnanbaloch. Edited November 14, 2013 by Jos 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...
Starstar Posted November 15, 2013 Share Posted November 15, 2013 (edited) .. and you for resurrecting a 7+ years old post to share your gratitude my dear adnanbaloch. Time is not a matter. i took benefit from it today therefore i thanks it imigatly to appreciate someone. and my thanks is a hint for other that this code is working perfectly. Edited November 15, 2013 by Starstar Life is like a coin. You can spend it Anyway as you wish and for your kind information. "you can spend it only once." Link to comment Share on other sites More sharing options...
Developers Jos Posted November 15, 2013 Developers Share Posted November 15, 2013 Well _Singleton() is the way to go and part of the Standard UDF library for quit a while. There are many threads that contain that answer and I hope your are not planning to resurrect them all just because you think they are useful. 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...
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