Page 1 of 1

[SOLVED] What's wrong with my RunProgram?

Posted: Sun Jun 01, 2025 10:03 pm
by Randy Walker
I expect this to open Notepad but it doesn't AND I expect it to post "Not Running" when I close Notepad. It does neither.

Code: Select all

notepad$ = "C:\Windows\System32\notepad.exe"
If FileSize(notepad$) >0
  Debug "Something"
\ONELOG\TERA+DEB.INI"
    p=RunProgram(notepad$,"","",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
    ;p=RunProgram(notepad$,"","") ; Opens if I uncomment and comment above.
    If p
      While IsProgram(p)
        Debug "Running"
        Delay(1000)
      Wend
        Debug "Stopped Running"
    EndIf
EndIf
BTW: I'm not trying to wait for the app to end. I'm trying to work up to a point where I can wait a few seconds to check and see if the app actually did open. Here the "p = RunProgram()" suggests it did open, but it did not open. Why?

Re: [Solved] What's wrong with my RunProgram?

Posted: Sun Jun 01, 2025 10:35 pm
by Randy Walker
I'm such an idiot. (headbang)
Duhh!! Ok, so i removed the #PB_Program_Hide and it opened fine. But does not post "not running" when I close Notepad. Why?

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Sun Jun 01, 2025 10:51 pm
by kenmo
IsProgram() tells you it's valid, ProgramRunning() tells you whether it actually finished.

(untested code edit)

Code: Select all

notepad$ = "C:\Windows\System32\notepad.exe"
If FileSize(notepad$) >0
  Debug "Something"
    p=RunProgram(notepad$,"","",#PB_Program_Open|#PB_Program_Read)
    ;p=RunProgram(notepad$,"","") ; Opens if I uncomment and comment above.
    If p
      While ProgramRunning(p)
        Debug "Running"
        Delay(1000)
      Wend
        Debug "Stopped Running"
      CloseProgram(p)
    EndIf
EndIf

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Sun Jun 01, 2025 11:22 pm
by moulder61
@Randy Walker
I know very little about this stuff, but I had a play with it to see if I could get it running? To make it interesting, I'm using Linux!
Anyway, not sure about the flags, but #Program_Read according to the help looks for console input. I'm guessing that's not what you want from a gui application?
I chopped it about a bit, and to be honest, it works both ways, with or without flags.
Here's my code for Linux using Pluma as a text editor. It seems to do what you are asking.

Moulder.

Code: Select all

notepad$ = "/usr/bin/pluma"
If FileSize(notepad$) >0
  Debug "Something"
    p=RunProgram(notepad$,"","",#PB_Program_Open|#PB_Program_Read)
    ;p=RunProgram(notepad$,"","") ; Opens if I uncomment and comment above.
    Repeat
      While ProgramRunning(p)
        Debug "Running"
        Delay(1000)
      Wend
        Debug "Stopped Running"
      End
    ForEver
EndIf

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 6:16 am
by jacdelad
Your repeat/forever loop is senseless.

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 10:33 am
by NicTheQuick
Some programs (maybe even the new Notepad in Windows 11 which has tabs) work like this:
  • If no other instance is running it just starts the notepad.exe process and `ProgramRunning()` returns #True
  • If another instance is already running it just notifies that instance to open a new tab, then the process terminates and `ProgramRunning()` returns #False although notepad.exe is still running

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 10:34 am
by Axolotl
acc. to your example, you can use an additional flag:

Code: Select all

#PB_Program_Wait   ; Help: Wait Until the launched program quits
If you want your program to remain operable, the use of a timer or a thread is recommended.

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 2:23 pm
by moulder61
@jacdelad

I'm guessing you are talking to me?

As Randy says in his signature "I *never* claimed to be a programmer."

I'm a mechanical engineer. I get shit to work.

Moulder.

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 5:43 pm
by Randy Walker
kenmo wrote: Sun Jun 01, 2025 10:51 pm IsProgram() tells you it's valid, ProgramRunning() tells you whether it actually finished.

(untested code edit)
You are quite right. IsProgram() was the wrong tool and ProgramRunning() works perfectly as illustrated by testing your "untested" code. THANKS!!!!

Re: [HALF Solved] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 6:09 pm
by Randy Walker
jacdelad wrote: Mon Jun 02, 2025 6:16 am Your repeat/forever loop is senseless.
:lol: Ha Ha Ha Indubitably it is, "senseless". :oops:

Re: [SOLVED] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 6:22 pm
by Randy Walker
Now that Kenmo got me on the right track, I was able to create an actual code block to give me what I was looking for:

Code: Select all

notepad$ = "C:\Windows\System32\notepad.exe"
If FileSize(notepad$) >0
  Debug "Something"
  exe=RunProgram(notepad$,"","",#PB_Program_Open|#PB_Program_Read)
  If exe
    While ProgramRunning(exe)
      trip + 1
      Debug trip
      Debug "Running"
      Delay(1000)
      If trip > 14
        Break
      EndIf
    Wend
    ;CloseProgram(p)
  EndIf
  If trip < 15
    Debug "program was killed"
  Else
    Debug "program was NOT killed"
  EndIf
EndIf
NOTE: No senseless repeat:forever loop. :)
PS: I'm actually using this to test if some stupid piece of shit antivirus is killing a necessary program launch.

Re: [SOLVED] What's wrong with my RunProgram?

Posted: Mon Jun 02, 2025 8:15 pm
by AZJIO
WinGetHandle

Code: Select all

Define hwnd
Define notepad$, trip

notepad$ = "C:\Windows\System32\notepad.exe"
If FileSize(notepad$) > 0
	Debug "Exists"
	If RunProgram(notepad$)
		Delay(500)
		While WinGetHandle(0, "Notepad", 1, #Class, 0)
			trip + 1
			Debug trip
			Debug "Running"
			Delay(1000)
			If trip > 14
				Break
			EndIf
		Wend
	EndIf
	If trip < 15
		Debug "program was killed"
	Else
		Debug "program was NOT killed"
	EndIf
EndIf

Re: [SOLVED] What's wrong with my RunProgram?

Posted: Tue Jun 03, 2025 12:53 am
by Randy Walker
AZJIO wrote: Mon Jun 02, 2025 8:15 pm WinGetHandle

Code: Select all

Define hwnd
Define notepad$, trip

notepad$ = "C:\Windows\System32\notepad.exe"
If FileSize(notepad$) > 0
	Debug "Exists"
	If RunProgram(notepad$)
		Delay(500)
		While WinGetHandle(0, "Notepad", 1, #Class, 0)
			trip + 1
			Debug trip
			Debug "Running"
			Delay(1000)
			If trip > 14
				Break
			EndIf
		Wend
	EndIf
	If trip < 15
		Debug "program was killed"
	Else
		Debug "program was NOT killed"
	EndIf
EndIf
No idea what your point is here, unless you think the overblown WinGetHandle() thing should be of any use.

Re: [SOLVED] What's wrong with my RunProgram?

Posted: Tue Jun 03, 2025 8:33 am
by BarryG