Jump to content

This should be simple.


ShawnW
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • Developers

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

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

  • Developers

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

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

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.

Link to comment
Share on other sites

  • Developers

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

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

  • Developers

You're right !

#include <Misc.au3>

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

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

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

  • Developers

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

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

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

Link to comment
Share on other sites

  • Developers

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

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

@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

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