ederhj

_Singleton; 1 instance; but newest one

13 posts in this topic

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

Share this post


Link to post
Share on other sites



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?

Share this post


Link to post
Share on other sites

Change the source in your script and restart?

Maybe you could first try to explain what you are trying to do here.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by MilesAhead

Share this post


Link to post
Share on other sites

Hi MilesAhead, Prodigy's just the title, im mpower :P. But yes I agree on the improvements, it was just a quick rough example to show elderhj that it can be done :)

Share this post


Link to post
Share on other sites

Hi MilesAhead, Prodigy's just the title, im mpower :P. 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.  :)

Share this post


Link to post
Share on other sites

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

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

 

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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hello Exit,

wow, awesome.

It works perfect.

Thank U

Bye

ederhj

 

Happy to service :thumbsup:

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

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