Jump to content

Multiple instance with limit


Recommended Posts

Hello. I need to allow multiple instance of the same script, now are disabled, but with some limitation

1) Not from the same dir. Example if i launch C:\Test.exe you can't run again C:\Test.exe but a copy of that in another folder like C:\Folder\Test.exe. The reason is the .dat i use for configuration, i don't what to share the same file with multiple instance. Every instance need to use a separate .dat but with the same name.

2) I need to know the number of the instance. Example a MsgBox from the C:\Test.exe return "Hello i'm number 1" the other one return "Hello i'm number 2" and so on

Some advice-script-direction? Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

What about this

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=SciTE.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <array.au3>
#include <WinAPIProc.au3>

_CheckForRunning()


While 1
    Sleep(100)
WEnd

Func _CheckForRunning()
    Local $av_Process = ProcessList('SciTE.exe')
    Local $i_TotalOccurence = $av_Process[0][0]
    Local $i_Limit = 3

    If $i_TotalOccurence > $i_limit Then ; check for max instances
        MsgBox(0, $i_TotalOccurence, 'limit reached')
        Exit
    EndIf

;~ _ArrayDisplay($av_Process)

    Local $s_ProcessFilePath = '' ; path of the running instance
    Local $s_StartingFilePath = @ScriptFullPath ; path of the new instance
    Local $i_RunProgram = 0 ; determine whether to run new instance (No)

    For $i = 1 To $i_TotalOccurence
        If $av_Process[$i][1] <> @AutoItPID Then ; skip self checking
            $s_ProcessFilePath = _WinAPI_GetProcessFileName($av_Process[$i][1])
            MsgBox(0, $av_Process[$i][0], 'I am instance ' & $i & @CRLF & 'Me        : ' & $s_StartingFilePath & @CRLF & 'Running: ' & $s_ProcessFilePath)

            If $s_StartingFilePath = $s_ProcessFilePath Then
                MsgBox(0, 'Shall I Run?', 'No')
                ExitLoop ; skip if path matches one already running
            EndIf
        EndIf

        If $i = $i_TotalOccurence Then $i_RunProgram = 1 ; all paths checked and no dupes running, allow new instance
    Next

    If Not $i_RunProgram Then Exit

    MsgBox(0, 'Shall I Run?', 'Yes')
EndFunc   ;==>_CheckForRunning

 

Edited by benners
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...