Sign in to follow this  
Followers 0
ShawnW

This should be simple.

14 posts in this topic

I have a script that may run more than once instance at the same time. I want the script to sleep until other instances are done. I was thinking something like this would work.

$ScriptsRunning = ProcessList("script.exe")
For $i=1 To $ScriptsRunning [0][0]
    $currentScriptsPID = ; ??? How do I get this if it might share a name with other instances of the process.
    If $ScriptsRunning [$i][1] <> $currentScriptsPID Then ProcessWaitClose($ScriptsRunning [$i][1])
Next

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Looking for @AutoItPID ?

One thing to remember is that 2 extra instances of a scripts waiting for the first one will get into a deadlock situation.

Edited by Jos

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You can also use a condition like this Posted Image

#include <Misc.au3>

Do 
    Sleep ( 50 )
Until Not _Singleton ( @ScriptName, 1 )

Did you test this?

The Until logic is incorrect and has the same deadlock issue.

Edited by Jos

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

Share this post


Link to post
Share on other sites

One thing to remember is that 2 extra instances of a scripts waiting for the first one will get into a deadlock situation.

Yeah thought of that which is why each instance gets a list of others with the same name at the time it starts. It only waits for those instances (minus itself) to stop. The next instance will have a larger array of processes to wait for, but the one currently running will not see any that start after it.

Share this post


Link to post
Share on other sites

Yeah thought of that which is why each instance gets a list of others with the same name at the time it starts. It only waits for those instances (minus itself) to stop. The next instance will have a larger array of processes to wait for, but the one currently running will not see any that start after it.

Yea that is true and should work indeed as long as you do not refresh the process array.

Jos


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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Did you test this?

The Until logic is incorrect and has the same deadlock issue.

You're right !

#include <Misc.au3>

Do 
    Sleep ( 50 )
Until _Singleton ( @ScriptName, 1 )=1
Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

You're right !

#include <Misc.au3>

Do 
    Sleep ( 50 )
Until _Singleton ( @ScriptName, 1 )=1

It is still wrong... try it :graduated:

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

Share this post


Link to post
Share on other sites

It is still wrong... try it :graduated:

Ok ! you win ! Posted Image

I have tried in a while loop with differents way but it doesn't want to work !

This function is supposed to indicate if another process with the same name is running !

That's strange...Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Simply:

#include <Misc.au3>

Do
    Sleep ( 50 )
Until _Singleton ( @ScriptName, 1 )
MsgBox(1,"test",_Singleton ( @ScriptName, 1 ))

_Singleton() returns a handle, not 1.

Compile it and run it 3 times.... you will see 1 msgbox and after you close it it will have a deadlock situation with the other 2 instances.

Jos

Edited by Jos

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

Share this post


Link to post
Share on other sites

Simply:

#include <Misc.au3>

Do
    Sleep ( 50 )
Until _Singleton ( @ScriptName, 1 )
MsgBox(1,"test",_Singleton ( @ScriptName, 1 ))

_Singleton() returns a handle, not 1.

Compile it and run it 3 times.... you will see 1 msgbox and after you close it it will have a deadlock situation with the other 2 instances.

Jos

So there is no solution ?

Or he add a condition to determine the winner ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

So there is no solution ?

Or he add a condition to determine the winner ! Posted Image

The original posted code should work fine when modified comparing to @AutoItPid.

The Second instance of script will then wait for only the first one.

The third instance will wait for both the first and second instance.

etc...


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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

@Jos

The original posted code should work fine when modified comparing to @AutoItPid.

The Second instance of script will then wait for only the first one.

The third instance will wait for both the first and second instance.

etc...

I'm ok with the deadlock situation...

but i'm not with your script example

#include <Misc.au3>Do Sleep ( 50 )Until _Singleton ( @ScriptName, 1 )MsgBox(1,"test",_Singleton ( @ScriptName, 1 ))

_Singleton() returns a handle, not 1.

Compile it and run it 3 times.... you will see 1 msgbox and after you close it it will have a deadlock situation with the other 2 instances.

your script say : Sleep until another handle is detected, but what ShawnW want it's : Sleep until there is no other process with same name !

And the first instance exit because it detect one of 2 others, ok

but why after the 2 others are not detected themself ? Posted Image

If you kill manually one, the last don't stop and is well waiting for another process !

that's not what ShawnW want !

So i tried like that with the string function for well compare return value from handle or zero

and a GUICtrlCreateEdit for get return in "live"

[/size][/color][/size]
[size="3"][color="#1C2837"][size=2]
 #include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
Local $myedit, $msg
GUICreate ( "My GUI edit", 250, 250 ) 
$myedit = GUICtrlCreateEdit ( "", 20, 20, 210, 210, $ES_AUTOVSCROLL+ $WS_VSCROLL )
GUISetState ( )

Sleep ( 4000 )

While 1
    $_Singleton = String ( _Singleton ( @ScriptName, 1 ) )
    GUICtrlSetData ( $myedit, $_Singleton & @CRLF, 1 )
    $_StringLen = StringLen ( $_Singleton )
    GUICtrlSetData ( $myedit, '$_StringLen : ' & $_StringLen & @CRLF, 1 )
    If $_StringLen <> 1 Then ; other process like 0x0000011C
        Sleep ( 50 )
    Else
        GUICtrlSetData ( $myedit, 'exiting' & @CRLF, 1 )
        Exitloop ; no other process with same name
    EndIf
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

MsgBox ( 1, "test", $_Singleton )
GUIDelete ( )
Exit[/size][/color][/size]
[size="3"][color="#1C2837"][size=2]

Like Jos said Compile it and run it 3 times....

So if you try this you will see the first execution detect once a handle but after exit because it detect nothing...

And after the 2 others instances detect nothing ( even one time ) and exit...

So, _Singleton function is strange !

I don't know if you understand my demonstration but i think this function is not clear ! Posted Image

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Never tried the _Singleton since as Jos said, my original code worked once he helped me find the @AutoItPID macro.

By the way I forgot to say thanks!

-Shawn

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