Jump to content

Running only one instance of AutoIt


traxxer
 Share

Recommended Posts

Hi there

We have an environment where we launch AU scripts via logon script, via RunOnce registry key or where different applications may start AU scripts.

Of course, sometimes different AU scripts mess themselfes up when running simultaneously.

How can I prevent an AU script from running, when another instance of AutoIt is already active?

Thanks very much in advance!

Michael

----

AutoIt v3.3.0.0 / WinXP SP3

Edited by traxxer
Link to comment
Share on other sites

Hi,

if you have compiled exe files, store them in a text file -> maybe on your netlogon share.

e.g. proclist.txt:

mapdrive.exe
checkgroup.exe

Use _FileReadToArray to store the exe files into an array.

Then loop over array and check with ProcessExist (<arrayelement>) if the process exists.

If yes, exit your script.

#include <file.au3>
_checkproc ()

Func _checkproc ()
    Local $arprocess
    Local $logonsvr = EnvGet ("logonserver")
    Local $procfile = $logonsvr & "\netlogon\proclist.txt"
    _FileReadToArray ($procfile, $arprocess)
    For $i = 1 To UBound ($arprocess) - 1
         If StringInStr ($arprocess [$i], @Scriptname) Then ContinueLoop
         If ProcessExists ($arprocess [$i]) Then Exit
    Next
EndFunc

;-))

Stefan

@Edit: Insert If StringInStr ($arprocess [$i], @Scriptname) Then ContinueLoop to prevent script from closing itself.

If you want to wait until a running process ends:

#include <file.au3>
_checkproc ()

Func _checkproc ()
    Local $arprocess
    Local $logonsvr = EnvGet ("logonserver")
    Local $procfile = $logonsvr & "\netlogon\proclist.txt"
    _FileReadToArray ($procfile, $arprocess)
    For $i = 1 To UBound ($arprocess) - 1
        If StringInStr ($arprocess [$i], @Scriptname) Then ContinueLoop
        If ProcessExists ($arprocess [$i]) Then 
            $pid = ProcessExists ($arprocess [$i])
            While $pid <> 0
                sleep (100) ; to reduce CPU
                $pid = ProcessExists ($arprocess [$i])
            WEnd
            ;_checkproc () ;you may use recursive level, but be careful because of recursive level restrictions
        EndIf
    Next
EndFunc
Edited by 99ojo
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...