Sign in to follow this  
Followers 0
nobbe

Program Starter

3 posts in this topic

hi

i need a program starter to control several programs

sets up dynamically the button controls from ini file too, so this may be useful for someone..

this is my first little start on it, you need to setup an "settings.ini" file as described in top of file

; setup ini file like "settings.ini"

#cs
[Settings1]
name=irfanview
verz_exe=C:\Programme\IrfanView\i_view32.exe
verz_ausfuerung=C:\Programme\IrfanView
autostart=1

[Settings2]
name=Total Commander
verz_exe=C:\Programme\totalc11md\TOTALCMD.EXE
verz_ausfuerung=C:\Programme\totalcmd\
autostart=1
#ce


#include <GUIConstants.au3>
#include <Array.au3>
#include <Process.au3>
#include <WinAPI.au3>

; ------------------- Options ----------------------------------

; Options , nicht den dialog mit esc schliessen
Opt("GUICloseOnESC", 0)
Opt("RunErrorsFatal", 0) ;; silent ; set @error to 1 !!

Global $glob_inilocation = @ScriptDir & "\settings.ini" ; generelles INI - einzelne inis folgen noch !

Global $glob_max_processes = 100; start a max number --

Dim $process_id[$glob_max_processes];       ## process iD für start / stop.
Dim $process_name[$glob_max_processes];     ## name "view.exe"
Dim $process_path[$glob_max_processes];     ## \pathname\..
Dim $process_running[$glob_max_processes];      ## runnning true / false??
Dim $process_autostart[$glob_max_processes] ; ## Autostart it ??

Dim $checkbox[$glob_max_processes];     ## checkbox if process is to be started ?
Dim $btn_start[$glob_max_processes];        ## button start this process
Dim $btn_stop[$glob_max_processes];     ## button stop this process

Global $glob_nbr_entries = 0; total number in array ..

; ---

;##
;##

#Region ### START Koda GUI section ### Form=datei_starter.kxf
$Form1 = GUICreate("Datei Starter", 684, 767, 193, 115)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section 



; read in settings ..
; generate dynamic labels after start of gui !!
_read_ini_file()

; ------ start --------------------------------------------------------

Do
    $msg = GUIGetMsg()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            MsgBox(64, "Beenden", "Programm wird beendet und alle lauffenden Task geschlossen")
            ExitLoop
    EndSelect
    
    ;
    ; outside the case main loop !!
    ;
    ; all buttons now ??
    For $irun = 1 To $glob_nbr_entries
        
        ; stop button 1 ... x 
        If $msg = $btn_stop[$irun] Then

            If ProcessExists($process_id[$irun]) Then
                $rc = ProcessClose($process_id[$irun])
                
                GUICtrlSetState($btn_start[$irun], $GUI_ENABLE)
                GUICtrlSetState($btn_stop[$irun], $GUI_DISABLE)
                
            EndIf
        EndIf

        ; start button 1 ... x
        If $msg = $btn_start[$irun] Then

            $process_id[$irun] = Run($process_path[$irun])
            If @error <> 1 Then ; no error ??
                GUICtrlSetState($btn_start[$irun], $GUI_DISABLE)
                GUICtrlSetState($btn_stop[$irun], $GUI_ENABLE)
            EndIf ; no error
            
        EndIf
        
    Next

Until $msg = $GUI_EVENT_CLOSE


;
;
;
Func _read_ini_file()

    Local $ret = "";

    Local $y_start = 20;

    For $i = 1 To $glob_max_processes - 1 ; [settings1 - 99]

        $sett = "settings" & $i;

        ; read first entry ..
        $ret = IniRead($glob_inilocation, $sett, "name", "ERROR")
        If $ret <> "ERROR" Then
            $glob_nbr_entries = $i
            
            $process_name[$i] = $ret

            $val = "verz_exe";
            $process_path[$i] = IniRead($glob_inilocation, $sett, $val, "")

            $val = "autostart"
            $process_autostart[$i] = IniRead($glob_inilocation, $sett, $val, "")

            ; build gui..
            $checkbox[$i] = GUICtrlCreateCheckbox($process_name[$i], 16, $y_start, 440, 18)
            $btn_start[$i] = GUICtrlCreateButton("Start", 480, $y_start, 75, 20, 0)
            $btn_stop[$i] = GUICtrlCreateButton("Stop", 558, $y_start, 75, 20, 0)

            $y_start += 25 ;

            _DebugPrint("Name " & $process_name[$i]);

            ; is it autostart ???
            If $process_autostart[$i] > 0 Then
                
                $process_id[$i] = Run($process_path[$i])
                If @error <> 1 Then ; no error ??
                    
                    GUICtrlSetState($btn_start[$i], $GUI_DISABLE)
                    GUICtrlSetState($btn_stop[$i], $GUI_ENABLE)
                    
                    
                EndIf ; no error

            EndIf ;; autostart

        EndIf

    Next ; alle $i
EndFunc   ;==>_read_ini_file



;
; print s debug text in CONSOLE view of scite..
;
Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")

    ConsoleWrite($s_text & @LF & "+===========================================================" & @LF)

EndFunc   ;==>_DebugPrint

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I updated above script. Thanks.

; setup ini file like "settings.ini"

#cs
[Settings1]
name=irfanview
path_exe=C:\Programme\IrfanView\i_view32.exe
verz_ausfuerung=C:\Programme\IrfanView
autostart=1
#ce

TraySetIcon("ProgramStarter.ico")

Opt("TrayAutoPause", 0)
Opt("TrayOnEventMode", 1)

#include <GUIConstants.au3>
#include <Array.au3>
#include <Process.au3>
#include <WinAPI.au3>
#Include <Constants.au3> ; Demonstrates StdoutRead(),StderrRead
Local $aboutitem = TrayCreateItem("Show Window")
TrayItemSetOnEvent(-1, "showhidengui")

; ------------------- Options ----------------------------------

; Options , nicht den dialog mit esc schliessen
Opt("GUICloseOnESC", 0)
;Opt("RunErrorsFatal", 0) ; silent ; set @error to 1 !!

Global $glob_inilocation = @ScriptDir & "\settings.ini" ; generelles INI - einzelne inis folgen noch !

Global $glob_max_processes = 100; start a max number --

Dim $process_id[$glob_max_processes]         ;## process iD for start / stop.
Dim $process_name[$glob_max_processes]       ;## name "view.exe"
Dim $process_path[$glob_max_processes]       ;## \pathname\..
Dim $process_running[$glob_max_processes]    ;## running true / false??
Dim $process_autostart[$glob_max_processes]  ;## Auto start it ??

Dim $checkbox[$glob_max_processes]           ;## checkbox if process is to be started ?
Dim $btn_start[$glob_max_processes]          ;## button start this process
Dim $btn_stop[$glob_max_processes]           ;## button stop this process
Dim $btn_restart[$glob_max_processes]        ;## button restart this process

Global $glob_nbr_entries = 0; total number in array ..


#Region ### START Koda GUI section ### Form=Program_starter.kxf
$Form1          = GUICreate("Program Starter", 550, 767, 193, 115)
GUISetIcon      ("ProgramStarter.ico")
GUISetState     (@SW_SHOW)
#EndRegion ### START Koda GUI section
$Button_Hide    = GUICtrlCreateButton("Hide", 400, 8, 50,25)

; read in settings ..
; generate dynamic labels after start of GUI !!
_read_ini_file()

; ------ start --------------------------------------------------------

Do
    $msg = GUIGetMsg()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            ;MsgBox(64, "Stop", "Programm Then exit and closed all border lauff task")
            $exithide = MsgBox(1, "Exit/Hide", "Yes = Exit" & @LF & "Cancel = Hide",0,$Form1)
            If ($exithide == 1) Then ExitLoop
            If ($exithide == 2) Then GUISetState(@SW_HIDE,$Form1)
            ;ExitLoop
        EndSelect

    Select
        Case $msg = $Button_Hide
            GUISetState(@SW_HIDE,$Form1)
    EndSelect
    ;
    ; outside the case main loop !!
    ;
    ; all buttons now ??
    For $irun = 1 To $glob_nbr_entries

        ; stop button 1 ... x
        If $msg = $btn_stop[$irun] Then
            p_stop()
        EndIf

        ; start button 1 ... x
        If $msg = $btn_start[$irun] Then
            p_start()
        EndIf

        ; restart button 1 ... x
        If $msg = $btn_restart[$irun] Then
            p_stop()
            p_start()
        EndIf
    Next

;Until $msg = $GUI_EVENT_CLOSE
Until False


Func _read_ini_file()
    Local $ret = "";
    Local $y_start = 40;

    For $i = 1 To $glob_max_processes - 1    ; [settings1 - 99]

        $sett = "settings" & $i;

        ; read first entry ..
        $ret = IniRead($glob_inilocation, $sett, "name", "ERROR")
        If $ret <> "ERROR" Then
            $glob_nbr_entries = $i

            $process_name[$i] = $ret

            $val = "path_exe";
            $process_path[$i]      = IniRead($glob_inilocation, $sett, $val, "")

            $val = "autostart"
            $process_autostart[$i] = IniRead($glob_inilocation, $sett, $val, "")

            ; build gui..
            $checkbox[$i]    = GUICtrlCreateCheckbox($process_name[$i], 16, $y_start, 260, 18)
            $btn_start[$i]   = GUICtrlCreateButton("Start", 300, $y_start, 75, 20, 0)
            $btn_stop[$i]    = GUICtrlCreateButton("Stop", 378, $y_start, 75, 20, 0)
            $btn_restart[$i] = GUICtrlCreateButton("Restart", 466, $y_start, 75, 20, 0)
            GUICtrlSetState($btn_stop[$i], $GUI_DISABLE)

            $y_start += 25 ;

            _DebugPrint("Name " & $process_name[$i]);

            ; is it autostart ???
            If $process_autostart[$i] > 0 Then

                ;If FileExists($process_path[i])=0 Then MsgBox(4096, "File Error" , $process_path[i] & @CRLF & "Does NOT exists")
                $process_id[$i] = Run($process_path[$i])
                If @error <> 1 Then ; no error ??

                    GUICtrlSetState($btn_start[$i], $GUI_DISABLE)
                    GUICtrlSetState($btn_stop[$i], $GUI_ENABLE)
                    Sleep(6000)
                EndIf ; no error
            EndIf ;; autostart
        EndIf
    Next ; alle $i

    GUISetState(@SW_HIDE,$Form1)               ; Ths Script will hide GUI after all programms run on start. You can activate it from tray.

EndFunc   ;_read_ini_file


; print s debug text in CONSOLE view of scite..
Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite($s_text & @LF & "+===========================================================" & @LF)
EndFunc

Func showhidengui()
    GUISetState(@SW_SHOW,$Form1)
EndFunc


Func CloseProcessByPID($pid1)
    ; http://www.autoitscript.com/forum/topic/61571-simple-taskkill-script-solved/
    $runString = StringFormat("taskkill /PID %s", $pid1)
    ;MsgBox(0,"runstring",$runString)
    $run = Run($runString, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    While 1
        $line = StdoutRead($run)
        If @error Then ExitLoop
        If $line<>"" Then MsgBox(0, "Sucess:", $line,3000,$Form1)
    Wend
    If @error=0 Then Return 1

    While 1
        $line = StderrRead($run)
        If @error Then ExitLoop
        If $line<>"" Then MsgBox(0, "Error:", $line,3000,$Form1)
    WEnd
    If @error=0 Then Return 0
EndFunc

Func p_stop()
    If ProcessExists($process_id[$irun]) Then
        ;$rc = ProcessClose($process_id[$irun])
        $rc = CloseProcessByPID($process_id[$irun])
        GUICtrlSetState($btn_start[$irun], $GUI_ENABLE)
        GUICtrlSetState($btn_stop[$irun], $GUI_DISABLE)
    Else
        $pclosed = MsgBox(4,"Process does not exist", "Do you closed the process?  " & $process_path[$irun],5000,$Form1)
        If $pclosed == 6 Then
            GUICtrlSetState($btn_start[$irun], $GUI_ENABLE)
            GUICtrlSetState($btn_stop[$irun], $GUI_DISABLE)
        ElseIf $pclosed == 7 Then
            MsgBox(1,"PID Does Not Exist", "Please close the process manually.  "    & $process_path[$irun],5000,$Form1)
        EndIf
    EndIf

EndFunc

Func p_start()
    $process_id[$irun] = Run($process_path[$irun])
    If @error <> 1 Then ; no error ??
        GUICtrlSetState($btn_start[$irun], $GUI_DISABLE)
        GUICtrlSetState($btn_stop[$irun], $GUI_ENABLE)
    EndIf ; no error
EndFunc
Edited by love2u
1 person likes this

Share this post


Link to post
Share on other sites

love2u,

Did you notice that this thread dates from over 6 years ago and that the OP has not been online for over 2 years? :huh:

Please do not necro-post like this again - just open a new thread and link to the old one if it is absoutely necessary for understanding the problem. We ask you to do this for two main reasons:

 

- 1. The language advances and the functionality might well be included in core or UDF code by now

- 2. The changes in language syntax mean that it is likely that code from more than a couple of years ago may well not run under the current release interpreter without significant modification - as you have proved above. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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