Jump to content

prevent multiple runs, is that possible?


Recommended Posts

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 by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

From memory, FAQ Number 14 :D

Absolutely correct, however there is a better way in the beta: _Singleton().

Cheers

Kurt

__________________________________________________________(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

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

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.

:D

Ouch...

@squirrely

Just do what Valik says and reply to this saying he is totally right and you'll never suggest a method like this again

EDIT:

Or don't reply at all

Edited by Paulie
Link to comment
Share on other sites

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

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

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

  • 7 years later...
  • Developers

Thanks a lot of you Squirrely1........

.. and you for resurrecting a 7+ years old post to share your gratitude my dear adnanbaloch.

Edited 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

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

  • Developers

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

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