ederhj Posted March 15, 2015 Share Posted March 15, 2015 Hello, I think I have a problem, that probably have more of you. First of all I want that only one instance of my script (au3 and not a compiled on) is running. But if I change the source-Code I want to kill the old script und start the newer script. Second. If I change my script in the source-code I want to reload it. Reload doesn't work easy. I tried to do it by a function, where I check if the modification-date of the file has changed. But I can't check, if theres anonther script running. Can you help me? Thank U. Bye Link to comment Share on other sites More sharing options...
mpower Posted March 15, 2015 Share Posted March 15, 2015 From what I can gather, what you're talking about is some form of an Auto-Update function. I.e. you distribute/run your Script version 1.0 and if an update is available e.g. version 1.1. you would like kill the existing version 1.0 script and update to the new version 1.1. Is that the case? Link to comment Share on other sites More sharing options...
Developers Jos Posted March 15, 2015 Developers Share Posted March 15, 2015 Change the source in your script and restart? Maybe you could first try to explain what you are trying to do here. 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...
JohnOne Posted March 15, 2015 Share Posted March 15, 2015 Don't know about the _Singleton part, I've never been able to figure that out, not that I've tried much. But you could check a hash of the file (@ScriptFullPath) in your main loop, and ShellExecute(@ScriptFullPath) and Exit. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
ederhj Posted March 16, 2015 Author Share Posted March 16, 2015 From what I can gather, what you're talking about is some form of an Auto-Update function. I.e. you distribute/run your Script version 1.0 and if an update is available e.g. version 1.1. you would like kill the existing version 1.0 script and update to the new version 1.1. Is that the case? Yes, this is the case. If I double-click on the au3-file i want the script to check, if another "instance" of the script is running. if so I want to stop the old script und start the new one. Thank U Link to comment Share on other sites More sharing options...
mpower Posted March 16, 2015 Share Posted March 16, 2015 Yes, this is the case. If I double-click on the au3-file i want the script to check, if another "instance" of the script is running. if so I want to stop the old script und start the new one. Thank U This works: #include <Misc.au3> #include <GUIConstants.au3> If _Singleton("apples are good", 1) = 0 Then MsgBox(0, 'death comes', 'kiiling previous running script!') $aProc = ProcessList(StringRegExpReplace(@AutoItExe, ".*\\", "")) For $i = 0 To UBound($aProc) - 1 If $aProc[$i][1] <> @AutoItPID Then ProcessClose($aProc[$i][1]) Next EndIf GUICreate('Example', 200, 200, -1, -1) $input = GUICtrlCreateInput('1', 50, 50, 100, 100, $ES_READONLY) AdlibRegister("Count", 1000) GUISetState() While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE Exit EndSwitch Wend Func Count() GUICtrlSetData($input, GUICtrlRead($input) + 1) EndFunc Link to comment Share on other sites More sharing options...
MilesAhead Posted March 16, 2015 Share Posted March 16, 2015 (edited) A couple of suggestions to improve the example by Prodigy: 1) Use a guid string instead of some "unique" string picked out of the air. Less chance of a collision. 2) If Singleton returns 0 check @error to make sure it is the case that the script already exists Const $ERROR_ALREADY_EXISTS = 183 If _Singleton("{343ADED1-5B6A-482D-BBE4-3541A17F9B90}", 1) = 0 Then If @error = $ERROR_ALREADY_EXISTS Then KillOtherInstances() Else OnAutoItExitRegister("_Cleanup") EndIf where KillOtherInstances() does what Prodigy does in his code to kill all but the current instance and _Cleanup is some optional cleanup code performed on exit. You can use a Guid generator to create a unique guid string for each program. I find it also handy for stuff like unique kernel object names like memory mapped files or mutexes. It guarantees the string is really unique. Edit: My code could be irmproved to add what to do if @error is something other than $ERROR_ALREADY_EXISTS but I guess I've gotten by with it as is. It seemed like every time I tried to make that section of the If a multi-line If on its own it woud not work as expected. So I just went with the single line version. Edited March 16, 2015 by MilesAhead My Freeware Page Link to comment Share on other sites More sharing options...
mpower Posted March 16, 2015 Share Posted March 16, 2015 Hi MilesAhead, Prodigy's just the title, im mpower . But yes I agree on the improvements, it was just a quick rough example to show elderhj that it can be done Link to comment Share on other sites More sharing options...
MilesAhead Posted March 16, 2015 Share Posted March 16, 2015 Hi MilesAhead, Prodigy's just the title, im mpower . But yes I agree on the improvements, it was just a quick rough example to show elderhj that it can be done Sorry, I've been away from the forum for awhile. Got to get my feet wet again. My Freeware Page Link to comment Share on other sites More sharing options...
Solution Exit Posted March 16, 2015 Solution Share Posted March 16, 2015 (edited) Did you tried the _SingleScript() function? #include <_SingleScript.au3> ;https://www.autoitscript.com/forum/topic/178681-_singlescript-assure-that-only-one-script-with-the-same-name-is-running _SingleScript() ; kill all scripts with the same name (AU3 and EXE) ; your code follows here Edited March 9, 2019 by Exit point to new #include location App: Au3toCmd UDF: _SingleScript() Link to comment Share on other sites More sharing options...
ederhj Posted March 16, 2015 Author Share Posted March 16, 2015 This works: #include <Misc.au3> #include <GUIConstants.au3> If _Singleton("apples are good", 1) = 0 Then MsgBox(0, 'death comes', 'kiiling previous running script!') $aProc = ProcessList(StringRegExpReplace(@AutoItExe, ".*\\", "")) For $i = 0 To UBound($aProc) - 1 If $aProc[$i][1] <> @AutoItPID Then ProcessClose($aProc[$i][1]) Next EndIf GUICreate('Example', 200, 200, -1, -1) $input = GUICtrlCreateInput('1', 50, 50, 100, 100, $ES_READONLY) AdlibRegister("Count", 1000) GUISetState() While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE Exit EndSwitch Wend Func Count() GUICtrlSetData($input, GUICtrlRead($input) + 1) EndFunc Hello, thank u. But the "$aProc[$i][1] <> @AutoItPID" doesn't work the way I want. This command deletes all AU3. I only wanted the right AU3 to be closed. On my PC there are running more than one script. Bye Link to comment Share on other sites More sharing options...
ederhj Posted March 16, 2015 Author Share Posted March 16, 2015 Did you tried the _SingleScript() function? May be found here or >here. #include <_SingleScript.au3> ;http://autoit.oo3.co/user/autoit/UDF/_SingleScript.au3 _SingleScript() ; kill all scripts with the same name (AU3 and EXE) ; your code follows here Hello Exit, wow, awesome. It works perfect. Thank U Bye ederhj Link to comment Share on other sites More sharing options...
Exit Posted March 16, 2015 Share Posted March 16, 2015 (edited) Hello Exit,wow, awesome.It works perfect.Thank UByeederhj Happy to service Edited March 16, 2015 by Exit App: Au3toCmd UDF: _SingleScript() 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