Jump to content

How to check if exemple.au3 is running


Jacov
 Share

Recommended Posts

Hi.
From one script I run other au3 scripts and exe programs.
When in the main script I choose to run all the programs and scripts , I want it will run only programs and scripts which are not running at this moment.
I tried to use _Singleton , but this command know only about scripts which I ran (and stopped)  from the main script.
If I already have exemple.au3 running , and I start the main script (and try to run exemple.au3 from the main script) , _Singleton doesn't see that the exemple.au3 already running. 
Is I close exemple.au3 from tray , manually, _Singleton sees the script exemple.au3 as running.

How can I know about running scripts ?

 

Link to comment
Share on other sites

  • Developers

There are several ways you can check. One could be to set the title of the Hidden AutoIt3 Window to something unique per script and test for the existence of that hidden window.

 

; put this in script one at the start somewhere
AutoItWinSetTitle("Script-one")

; Add this test in the Master script
If WinExists("Script-one") then
   ; logic here
EndIf

.... but guess you could also get this to work with _Singleton(), so show us an example of your implementation that's not working.

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

11 minutes ago, Jos said:

There are several ways you can check. One could be to set the title of the Hidden AutoIt3 Window to something unique per script and test for the existence of that hidden window.

 

; put this in script one at the start somewhere
AutoItWinSetTitle("Script-one")

; Add this test in the Master script
If WinExists("Script-one") then
   ; logic here
EndIf

.... but guess you could also get this to work with _Singleton(), so show us an example of your implementation that's not working.

Hi thanks, WinExists works.

My example:

Main.au3:

Func RunManyprogramsStart()
    Local Const $sFilePath = @AppDataDir & "\tiouta\ListOfPrograms.txt"
    Local $aRetArray
    Local $FILEname
   _FileReadToArray($sFilePath, $aRetArray, $FRTA_NOCOUNT)
   $iRows = UBound($aRetArray)
   $tValue = DllStructCreate("dword")
   If @OSArch = "X64" And @AutoItX64 = 0 Then
        DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "ptr", DllStructGetPtr($tValue))
        For $i = 0 To $iRows - 1
            my_run($aRetArray[$i])
        Next
        DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "ptr", DllStructGetPtr($tValue))
   Else                          ; this case -> @OSArch = "X64" And @AutoItX64 = 1   and others
        For $i = 0 To $iRows - 1
            my_run($aRetArray[$i])
        Next
   EndIf
   SoundPlay(@WindowsDir & "\media\notify.wav", 1)
EndFunc


Func my_run($prog)
   $FILEname = StringMid($prog,StringInStr($prog,"\",2,-1) +1)
   $FILEname = StringReplace($FILEname, '"', '')
   $FILEname = StringReplace($FILEname, '.au3', '')

   ;If ProcessExists($FILEname) OR _Singleton($FILEname, 1) = 0 Then
   If ProcessExists($FILEname) OR WinExists($FILEname) Then
      Return
   EndIf
   $extension = StringReplace(StringRight($prog,4),".","")
   $extension = StringReplace($extension, '"', '')
   if $extension = "exe" Then
      Run($prog)
   ElseIf  $extension = "au3" Then
      ShellExecute($prog)
   Else
      MsgBox(64,@SCRIPTNAME,"Unknown program : "& $prog)
   EndIf
EndFunc

" If ProcessExists($FILEname) OR _Singleton($FILEname, 1) = 0 Then" - this one doesn't work properly , When I start Main.au3 and run function RunManyprogramsStart() , it will start all the programs in the list "ListOfPrograms.txt" , and if I will run the function again "if" by "_Singleton($FILEname, 1) = 0" will give "Return".
But if I close the scripts which appear in  the list "ListOfPrograms.txt" manually from tray , and I will run the function "RunManyprogramsStart()" again, the "if" with "_Singleton($FILEname, 1) = 0" will give "Return".

 

 
The similar problem if the scripts from the list "ListOfPrograms.txt" are running (I didn't close them) and I start "Main.au3" , and run the function "RunManyprogramsStart()" , it will try to run the scripts from the list "ListOfPrograms.txt" although them already running.
Link to comment
Share on other sites

There's a couple of things that you should check out. For your "my_run" functions beginning $FILEname actions, check out _PathSplit: https://www.autoitscript.com/autoit3/docs/libfunctions/_PathSplit.htm

Another way to check if a script is already running is _Singleton: https://www.autoitscript.com/autoit3/docs/libfunctions/_Singleton.htm if you put this into your AutoIt scripts/exes (before anything else) it can be setup (by default) to only allow one instance of a script to be running, so you don't even need to check if the process or window exists.

I'd also recommend to check out the best practices page for some variable naming tips: https://www.autoitscript.com/wiki/Best_coding_practices

We ought not to misbehave, but we should look as though we could.

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