Page 1 of 1

Programrunning doesn't work with batch programs

Posted: Sun Dec 01, 2024 4:30 pm
by artnat
If I start a batch file with RunProgram, a valid 1 is returned, but to find the end condition by ProgramRunning does not work. The error message is "The specified program is not valid". That is not very astonishing, since the batchfile starts another program, and what I want to find out is, when this program finishes, and not the batch itself. But does anybody know, how to get an end condition for the program, which is started by the batch file?

Re: Programrunning doesn't work with batch programs

Posted: Sun Dec 01, 2024 5:11 pm
by infratec
No code .... no help.

How should we know what you have done exactly?

Re: Programrunning doesn't work with batch programs

Posted: Sun Dec 01, 2024 5:33 pm
by TI-994A
artnat wrote: Sun Dec 01, 2024 4:30 pm...how to get an end condition for the program, which is started by the batch file?

Hi @artnat. If you know the name of the program, you could try this approach. This example tracks Notepad, so simply start and stop Notepad to see the progress:

Code: Select all

;
; originally posted in October 2012
; https://www.purebasic.fr/english/viewtopic.php?p=394076#p394076
; 

Procedure.i FindWindowTitle(tSearch.s)
  Protected hWnd.i, tWnd.s
  hWnd = GetWindow_(GetDesktopWindow_(), #GW_CHILD)
  While hWnd
    tWnd = Space(GetWindowTextLength_(hWnd) + 2)
    GetWindowText_(hWnd, tWnd, Len(tWnd))
    If FindString(LCase(tWnd), LCase(tSearch))
      ProcedureReturn 1
    EndIf
    hWnd = GetWindow_(hWnd, #GW_HWNDNEXT)
  Wend
  ProcedureReturn 0
EndProcedure

win = OpenWindow(#PB_Any, 0, 0, 400, 200, "Program Monitor (Windows only)",
                 #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
status = TextGadget(#PB_Any, 0, 80, 400, 30, "", #PB_Text_Center)
AddWindowTimer(win, 0, 1000)

Repeat
  event = WaitWindowEvent()  
  Select event
      
    Case #PB_Event_Timer
      If FindWindowTitle("Notepad")
        SetGadgetText(status, "Notepad is running...")
      Else        
        SetGadgetText(status, "Notepad is currently not running.")               
      EndIf
      
    Case #PB_Event_CloseWindow
      appQuit = #True
      
  EndSelect  
Until appQuit

Re: Programrunning doesn't work with batch programs

Posted: Sun Dec 01, 2024 6:06 pm
by artnat
I found a way, to start the program itself with the batchfile as parameter. The program (freefilesync) runs fine, and the handle is 1, but I still get the error message "The specified "program" is not valid."

Code: Select all

handle.i=RunProgram("FreeFileSync.exe", "C:\Users\hd\OneDrive\Documents\prog\purebasic\dsich_neu\BatchRun.ffs_batch", "C:\Program Files\FreeFileSync\")
Debug handle

While ProgramRunning(handle)
  Debug handle
  Delay (1000)
Wend

Re: Programrunning doesn't work with batch programs

Posted: Sun Dec 01, 2024 6:12 pm
by TI-994A
artnat wrote: Sun Dec 01, 2024 6:06 pm I found a way, to start the program itself with the batchfile as parameter. The program (freefilesync) runs fine, and the handle is 1, but I still get the error message "The specified "program" is not valid."

Code: Select all

handle.i=RunProgram("FreeFileSync.exe", "C:\Users\hd\OneDrive\Documents\prog\purebasic\dsich_neu\BatchRun.ffs_batch", "C:\Program Files\FreeFileSync\")
Debug handle

While ProgramRunning(handle)
  Debug handle
  Delay (1000)
Wend

To get a valid handle, you might need to add the #PB_Program_Open flag, like so:

Code: Select all

handle.i=RunProgram("FreeFileSync.exe", "C:\Users\hd\OneDrive\Documents\prog\purebasic\dsich_neu\BatchRun.ffs_batch", "C:\Program Files\FreeFileSync\", #PB_Program_Open)