gsb Posted October 14, 2006 Share Posted October 14, 2006 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 More sharing options...
The Kandie Man Posted October 14, 2006 Share Posted October 14, 2006 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 More sharing options...
gsb Posted October 14, 2006 Author Share Posted October 14, 2006 Thank you The Kandie Man. It is tough finding the things I do not yet know about. I will look it now. gsb "Did you ever stop to think? ...and forget to restart!" Link to comment Share on other sites More sharing options...
Cue Posted October 15, 2006 Share Posted October 15, 2006 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 Link to comment Share on other sites More sharing options...
Paulie Posted October 15, 2006 Share Posted October 15, 2006 (edited) 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 elseTest 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 October 15, 2006 by Paulie Link to comment Share on other sites More sharing options...
Valik Posted October 15, 2006 Share Posted October 15, 2006 Test your scripts before posting.There are so many ways that could failEDIT: his script doesn't even have a window! how will that help him?@gsbUse '_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 More sharing options...
MHz Posted October 15, 2006 Share Posted October 15, 2006 (edited) EDIT: his script doesn't even have a window! how will that help him?Indeed it does have a window. Every AutoIt3 script has a hidden window. Edit: This will show the window WinSetState(AutoitWinGetTitle(), '', @SW_SHOW) Sleep(10000) Edited October 15, 2006 by MHz Link to comment Share on other sites More sharing options...
Paulie Posted October 15, 2006 Share Posted October 15, 2006 (edited) 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 workBut 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.@MHzLearn something new every day i guess... Edited October 15, 2006 by Paulie Link to comment Share on other sites More sharing options...
shaamaan Posted October 15, 2006 Share Posted October 15, 2006 Script - Limit to one copy running at a time so subsequent instantiations exit quietly ... try this: If @Compiled = 1 Then $list = ProcessList(@ScriptName) If $list[0][0] > 1 Then Exit EndIf Link to comment Share on other sites More sharing options...
gsb Posted October 15, 2006 Author Share Posted October 15, 2006 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 More sharing options...
Developers Jos Posted October 15, 2006 Developers Share Posted October 15, 2006 (edited) 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 October 15, 2006 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 More sharing options...
gsb Posted October 15, 2006 Author Share Posted October 15, 2006 Thank you. I shall leave it as _Singleton. gsb "Did you ever stop to think? ...and forget to restart!" Link to comment Share on other sites More sharing options...
Valik Posted October 15, 2006 Share Posted October 15, 2006 try this: If @Compiled = 1 Then $list = ProcessList(@ScriptName) If $list[0][0] > 1 Then Exit EndIfWorst way ever conceived. You really should be shot multiple times for even suggesting this (My standard reaction to that particular code). Link to comment Share on other sites More sharing options...
Cue Posted October 17, 2006 Share Posted October 17, 2006 (edited) Wow, seems that this post really heated up while i was away.@PaulieI know you did not mean to be harsh but to sayTest your scripts before posting.when infact you have not tested mine seems a bit hypocriticalyou also said EDIT: his script doesn't even have a window! how will that help him?later sayingi never meant to suggest the code wouldn't workwhich slightly contradicts what you said earlierAnyway, 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). Edited October 17, 2006 by Cue Link to comment Share on other sites More sharing options...
Paulie Posted October 17, 2006 Share Posted October 17, 2006 (edited) Wow, seems that this post really heated up while i was away.@PaulieI know you did not mean to be harsh but to saywhen infact you have not tested mine seems a bit hypocriticalyou also said later sayingwhich slightly contradicts what you said earlierAnyway, 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. 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 October 17, 2006 by Paulie Link to comment Share on other sites More sharing options...
Tentaal Posted November 11, 2006 Share Posted November 11, 2006 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 More sharing options...
Developers Jos Posted November 11, 2006 Developers Share Posted November 11, 2006 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 More sharing options...
Tentaal Posted November 11, 2006 Share Posted November 11, 2006 (edited) 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 November 11, 2006 by Tentaal Link to comment Share on other sites More sharing options...
Uten Posted November 11, 2006 Share Posted November 11, 2006 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. Please keep your sig. small! Use the help file. Search the forum. Then ask unresolved questions :) Script plugin demo, Simple Trace udf, TrayMenuEx udf, IOChatter demo, freebasic multithreaded dll sample, PostMessage, Aspell, Code profiling 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