Sign in to follow this  
Followers 0
Guillermo

Make sure only an instance is running

22 posts in this topic

... And after reading the help file and searching the forum I couldn't prevent my script from spawning copies of itself.

So I've come up with this:

$list = ProcessList()
$a=0
for $i = 1 to $list[0][0]
  If $list[$i][0] = "MyScript.exe" Then
      $a=$a+1
      EndIf
  next 
  
  If $a > 1 Then      

MsgBox(48,"","There's a copy of this program running already.")

      Exit
      EndIf

The funny thing is that I can see the MsgBox on my PC, but not on a colleague's computer IF he runs the script by means of a shortcut. If he clicks directly on the .exe, my message will appear. May that be linked with account rights? ;)

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

No, I've tried that, but it doesn't work [at all]. (Or at least I don't know how to make it work.)

Edited by Guillermo

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try using this line:

If UBound(ProcessList(@ScriptName)) > 2 then Exit

Hope this will help....

Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Nice alternative.

It works too on my computer, but not on my colleagues [(I mean: the msgbox will ony show on my computer.)]... :-?

p.s. Hell, I keep forgetting things... :-P

Edited by Guillermo

Share this post


Link to post
Share on other sites

See _singleton UDF if you can run with the beta release ;)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

FAQ#14 should work - but let's find out what the problem is with the colleague's system.

Make/run an au3 file (or compile it) that has only one line:

MsgBox(48,"","There's a copy of this program running already.")

Does that show up on your colleague's system?

If no, then we have a different rabbit to chase.

If yes, then run a script that contains only this code from the help file:

; List all processes

$list = ProcessList()

for $i = 1 to $list[0][0]

msgbox(0, $list[$i][0], $list[$i][1])

next

Grasping at straws here......

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

That method is completely flawed. FAQ item #14, although flawed as well is about 75% reliable however the chance of breaking it require an advanced user.

Search the forum for the word "singleton" and also match the search results to the name "Valik" and you'll find several posts by me on the subject and one of them explains why using the name of the script is one of the worst ways of trying to implement the singleton pattern that can be imagined.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

how to have only one instance of a script running

I guess that's the topic you mean, Valik.

However, as far as I understand, basing my check on the script's name will only fail if the user renames my script manually or copies it manually, isn't it?

I'm a hobbyist programmer, so probably I'm missing the point, but the thing is that for the intended use of my script Danny35d's suggestion or my initial code would suffice if that's so.

What I don't understand is why the damn MsgBox will show up on my system whereas not on others, both as compiled and uncompiled script.

[Don't ask me why, but today my script's running perfecly on every system. For the record, my colleague's computer has folders on the desktop that he's created and can't delete. I chose him as beta tester for a reason: the worst scenario can't be worse than his everyday pc. ;) ]

Edited by Guillermo

Share this post


Link to post
Share on other sites

how to have only one instance of a script running

I guess that's the topic you mean, Valik.

Yes.

However, as far as I understand, basing my check on the script's name will only fail if the user renames my script manually or copies it manually, isn't it?

Not quite. While it is true that renaming the script will allow a second instance to run, using your method may also prevent your script from ever running if there happens to be an identically named process running.

I'm a hobbyist programmer, so probably I'm missing the point, but the thing is that for the intended use of my script Danny35d's suggestion or my initial code would suffice if that's so.

I'm starting to get a bit sick of the "I'm a hobbyist so its okay for me not to do things right/efficiently/the best way I can." The point is you want to prevent multiple instances of your script from running. My point is that your method will only do that some of the time but there is an easier way to do it all the time.

Search the forum for _Singleton or download the latest beta to obtain a copy of the function (Its written in AutoIt and 3.1.1 compatible once you get it).

Share this post


Link to post
Share on other sites

I'm starting to get a bit sick of the "I'm a hobbyist so its okay for me not to do things right/efficiently/the best way I can." The point is you want to prevent multiple instances of your script from running. My point is that your method will only do that some of the time but there is an easier way to do it all the time.

Luckily, I'm a perfectionist too, so I've delved into the scaring depths of beta releases and UDF's to challenge myself.

And _Singleton works just perfectly!

But I guess this means more code to install the "misc.au3" file alongside my script to make it work on other users' systems, doesn't it? Maybe this is heresy, but I'd like to be able to copy the _Singleton function into my code and forget about installations, not matter how easy.

That last bit sounds rather conformist! ;)

Thank you very much indeed, Valik.

Share this post


Link to post
Share on other sites

If you compile your AutoIt script into an EXE then the _Singleton() function will work without bringing along the Misc.au3 file. If you are in fact just distributing a .AU3 file then you can copy the _Singleton() function from Misc.au3 to the end of your script.

Share this post


Link to post
Share on other sites

If you compile your AutoIt script into an EXE then the _Singleton() function will work without bringing along the Misc.au3 file. If you are in fact just distributing a .AU3 file then you can copy the _Singleton() function from Misc.au3 to the end of your script.

That's not true the includes are really included with the script when compiled to avoid external reference. ;)

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

...But that's what I said! ;)

But you have said it in a better fashion.

Edited by LxP

Share this post


Link to post
Share on other sites

...without bringing along the Misc.au3 file...

This could be interpreted as saying that only the function called will be complied into the script... and as you know, all functions within the included file will be "brought into" a complied script... even the functions that you do not need for the original script.

Sorry to repeat the point - but I did not think that this was clear.

It is probably just me.........Aint English grand?

Sorry jpm, "...the includes are really included..." just was not wordy enough for me. I just had to go for the overkill of this post.

Thanks for putting up with me today.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

This could be interpreted as saying that only the function called will be complied into the script... and as you know, all functions within the included file will be "brought into" a complied script... even the functions that you do not need for the original script.

Sorry to repeat the point - but I did not think that this was clear.

It is probably just me.........Aint English grand?

Sorry jpm, "...the includes are really included..." just was not wordy enough for me. I just had to go for the overkill of this post.

Thanks for putting up with me today.

I should have added that the compiled script will include the whole misc.au3 so the resulting size will be more than just include _singleton functions.

If you want to have a smaller compiled script you can just cut and paste the _singleton functions inside the script to be compiled.

;)

Share this post


Link to post
Share on other sites

Thanks for all your input, folks.

One more question, though this one has little to do whith the original topic...

Does it make any difference to place the script functions at the top or at the bottom of the code? :P

The help file says they usually go after the code, but isn't that a bit illogical? If the funcions are used IN the code, wouldn't it make more sense to place them at the very beginning so they are interpreted first?

Guillermo ;)

Share this post


Link to post
Share on other sites

Thanks for all your input, folks.

One more question, though this one has little to do whith the original topic...

Does it make any difference to place the script functions at the top or at the bottom of the code? :P

The help file says they usually go after the code, but isn't that a bit illogical? If the funcions are used IN the code, wouldn't it make more sense to place them at the very beginning so they are interpreted first?

Guillermo ;)

because the body of your code is executed before anything else, typically the others follow for readability. That way you can start reading at the beginning of execution which makes for easier stepping through the program...IMHO

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

because the body of your code is executed before anything else, typically the others follow for readability. That way you can start reading at the beginning of execution which makes for easier stepping through the program...IMHO

In terms of ease of use for humans, I understand that, but actually I meant the impact on efficiency when the script is run. Sorry if I haven't been clear enough. ;)

Share this post


Link to post
Share on other sites

In terms of ease of use for humans, I understand that, but actually I meant the impact on efficiency when the script is run. Sorry if I haven't been clear enough. ;)

I don't think there is any impact on performance. If the interpreter first finds a call to a yet unknown function it will try to find the user defined function and the code, as it needs it anyway to execute the code. After that, all the functions details are stored internally, so the next call to that funtion will be probably faster. So, I don't see a reason why it would make any difference where to put your function definition.

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 *

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  
Followers 0