[SOLVED] What's wrong with my RunProgram?

Just starting out? Need help? Post your questions and find answers here.
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

[SOLVED] What's wrong with my RunProgram?

Post 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?
Last edited by Randy Walker on Mon Jun 02, 2025 5:38 pm, edited 2 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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?
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
kenmo
Addict
Addict
Posts: 2043
Joined: Tue Dec 23, 2003 3:54 am

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

Post 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
User avatar
moulder61
Enthusiast
Enthusiast
Posts: 193
Joined: Sun Sep 19, 2021 6:16 pm
Location: U.K.

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

Post 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
"If it ain't broke, fix it until it is!

This message is brought to you thanks to SenselessComments.com

My PB stuff for Linux: "https://u.pcloud.link/publink/show?code ... z3MR0T3jyV
User avatar
jacdelad
Addict
Addict
Posts: 2007
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post by jacdelad »

Your repeat/forever loop is senseless.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
NicTheQuick
Addict
Addict
Posts: 1518
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

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

Post 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
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Axolotl
Addict
Addict
Posts: 835
Joined: Wed Dec 31, 2008 3:36 pm

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

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
moulder61
Enthusiast
Enthusiast
Posts: 193
Joined: Sun Sep 19, 2021 6:16 pm
Location: U.K.

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

Post 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.
"If it ain't broke, fix it until it is!

This message is brought to you thanks to SenselessComments.com

My PB stuff for Linux: "https://u.pcloud.link/publink/show?code ... z3MR0T3jyV
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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!!!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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:
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
AZJIO
Addict
Addict
Posts: 2183
Joined: Sun May 14, 2017 1:48 am

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

Post 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
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

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

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4170
Joined: Thu Apr 18, 2019 8:17 am

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

Post by BarryG »

Post Reply