Jump to content

Script - Limit to one copy running at a time


gsb
 Share

Recommended Posts

Script - Limit to one copy running at a time so subsequent instantiations exit quietly,

or maybe set the previous one's tray icon to blink for a few seconds and then exits

So I am learning by examples, help file and docs.

Question. Here is a most simple script.

#Include <Constants.au3>
#NoTrayIcon

TraySetToolTip("This is my new tooltip text!")
TraySetState()

While 1
    Sleep(10); Idle loop
WEnd

Exit

How can I insure that only one is running at a time?

That is, if by mistake I forget to exit the running script before I start the script again, I want the second instance to check and if there is already a copy running simply exit the script.

How would I set up such a check?

Thanks,

gsb

"Did you ever stop to think? ...and forget to restart!"
Link to comment
Share on other sites

Use the _Singleton function. Search for it in the documentation. This function makes it so that only one script can be run at a time.

Here is the example from the help file:

#include "Misc.au3"
if _Singleton("test\test",1) = 0 Then
    Msgbox(0,"Warning","An occurence of test is already running")
    Exit
EndIf
Msgbox(0,"OK","the first occurence of test is running")

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

you should have found in the FAQs

$Mytitle = "your window name"
If WinExists($Mytitle) Then Exit
AutoItWinSetTitle($Mytitle)

add this to the top of the script before anything else

Test your scripts before posting.

There are so many ways that could fail

EDIT: his script doesn't even have a window! how will that help him?

@gsb

Use '_Singleton'

Edited by Paulie
Link to comment
Share on other sites

Test your scripts before posting.

There are so many ways that could fail

EDIT: his script doesn't even have a window! how will that help him?

@gsb

Use '_Singleton'

While using Singleton() is the way to go, you should make sure you know what you're talking about, too, before you go lambasting somebody. Not only does the code work as demonstrated, it is the example in the help file for this very thing.
Link to comment
Share on other sites

While using Singleton() is the way to go, you should make sure you know what you're talking about, too, before you go lambasting somebody. Not only does the code work as demonstrated, it is the example in the help file for this very thing.

I understand, and i never meant to suggest the code wouldn't work

But the code was not really the best for the task the OP intended it to be used for, though it may work, it is easily thrown off by unpredictable things such as other windows.

If i was too harsh i apologize, but i don't think that basing something like this on window titles is a very reliable method. Especially when others (Singleton) exist.

@MHz

Learn something new every day i guess...

Edited by Paulie
Link to comment
Share on other sites

Sorry to cause trouble.

I am learning.

I did look through the FAQ and lots more so far.

But I did not know that such a script has a "hidden" named window.

_Singleton is working fine; but is shaamaan's code even better?

If @Compiled = 1 Then
   $list = ProcessList(@ScriptName)
   If $list[0][0] > 1 Then Exit
EndIf

Better as in efficiencies. These things I do not know yet.

Thank you everyone for your help.

I do appreciate it.

gsb

"Did you ever stop to think? ...and forget to restart!"
Link to comment
Share on other sites

  • Developers

Sorry to cause trouble.

I am learning.

I did look through the FAQ and lots more so far.

But I did not know that such a script has a "hidden" named window.

_Singleton is working fine; but is shaamaan's code even better?

It works assuming your program name is unique but with Singleton you are 100% sure about the fact that this program only runs once. Edited by JdeB

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

Wow, seems that this post really heated up while i was away.

@Paulie

I know you did not mean to be harsh but to say

Test your scripts before posting.

when infact you have not tested mine seems a bit hypocritical

you also said

EDIT: his script doesn't even have a window! how will that help him?

later saying

i never meant to suggest the code wouldn't work

which slightly contradicts what you said earlier

Anyway, I do not mean to cause an argument. I for 1 didnt know about _singleton and didnt consider the other option failing if another window with the same name already existed so thank you for pointing that out.

Worst way ever conceived. You really should be shot multiple times for even suggesting this (My standard reaction to that particular code).

:lmao: Edited by Cue
Link to comment
Share on other sites

Wow, seems that this post really heated up while i was away.

@Paulie

I know you did not mean to be harsh but to say

when infact you have not tested mine seems a bit hypocritical

you also said

later saying

which slightly contradicts what you said earlier

Anyway, I do not mean to cause an argument. I for 1 didnt know about _singleton and didnt consider the other option failing if another window with the same name already existed so thank you for pointing that out.

:lmao:

You're right, i did contradict myself, but when i said that, i didn't know that every script actually had a window that was just hidden.

But singleton is definitely the way to go. I use it often

Edited by Paulie
Link to comment
Share on other sites

  • 4 weeks later...

There is something I dont understand with _Singleton.

I have thos code

Func IsAlreadyRunning()
if _Singleton("Killwirelessv2.exe",1) = 0 Then
    Msgbox(0,"Error","Programm is already running.")
    Exit
EndIf
EndFunc

When I lauch killwirelessv2.exe a second time, I get the message "Programm is already running".

But.. If I change the code like this, for example :

if _Singleton("Killwir.exe",1) = 0 Then

it is still working!

Why??

Tnanks.

Link to comment
Share on other sites

  • Developers

There is something I dont understand with _Singleton.

I have thos code

Func IsAlreadyRunning()
if _Singleton("Killwirelessv2.exe",1) = 0 Then
    Msgbox(0,"Error","Programm is already running.")
    Exit
EndIf
EndFunc

When I lauch killwirelessv2.exe a second time, I get the message "Programm is already running".

But.. If I change the code like this, for example :

if _Singleton("Killwir.exe",1) = 0 Then

it is still working!

Why??

Tnanks.

Isn't that what you would expect ?

Maybe you can explain what it is you don't understand ?

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

Isn't that what you would expect ?

Maybe you can explain what it is you don't understand ?

What's the purpose of the string ($occurenceName) we must put between " "?

Help said

:

$occurrenceName string to identify the occurrence of the script .

Ok, but what is this occurence? Process name? exe name?

Edited by Tentaal
Link to comment
Share on other sites

Use the source Luke...

_Singleton uses CreateMutex. So $occurenceName must be a unique identifier.

If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

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