RunProgram() and get its output data
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
RunProgram() and get its output data
I want to show the external program output data while it is still running.
However i find no way to get output data from the external program before it ends and show it via a ListViewGadget for example. Why? because calling ReadProgramString/Data DOES WAIT until program is finished, and calling AvailableProgramOutput() will always return 0 until program is finished.
So then, how to do it???
However i find no way to get output data from the external program before it ends and show it via a ListViewGadget for example. Why? because calling ReadProgramString/Data DOES WAIT until program is finished, and calling AvailableProgramOutput() will always return 0 until program is finished.
So then, how to do it???
CODE: Piper.pb/piper.exe
CODE: Reader.pb/reader.exe
Code: Select all
OpenConsole()
Repeat
PrintN(Str(I))
Delay(500)
I + 1
ForEver
Code: Select all
OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ListViewGadget(0, 10, 10, 492, 364)
Pid = RunProgram("piper.exe", "", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide)
Repeat
Select WaitWindowEvent(10)
Case #PB_Event_CloseWindow
Break
EndSelect
If AvailableProgramOutput(Pid)
String.s = ReadProgramString(Pid)
AddGadgetItem(0, 0, String)
EndIf
ForEver
KillProgram(Pid)
CloseProgram(Pid)
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
This is definetely a bug. I tested with this Pascal program (compiled with Delphi) and the test code above (but removed the Hide flag):
When I run it appears like "nothing" happens. My program reacts normally. The pascal program shows an empty console. I then wait for some time. Suddenly, the numbers from 0 to 33 dumps into the listview!
Of course, one could blame this on piper.exe. However... Now my program does not react to gui input any more! This cannot be anything else than a bug in PB as the command ReadProgramString() (which waits and locks up the gui if the stream is empty) should ONLY be called when there is data in the stream. There is no other command in my program that would lock up the gui as far as I can tell.
Well, I wait a bit longer. Now another bunch of numbers dumps into the listview (ca 30.) Then I go away to write this. When I get back to my program it is still locked up for a while. Then it reacts normally and the listview has now 145 items. I then write here again, now the program won't react again. Now it reacts. Now I got a bunch of new numbers and it stopped reacting.
Edit: I stopped the program with the debugger's kill command (it didn't react again) and piper.exe crashed with an unknown exception.
Code: Select all
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
I: Integer = 0;
begin
repeat
Sleep(500);
WriteLn(I);
Inc(I);
until 1 = 0
end.
Of course, one could blame this on piper.exe. However... Now my program does not react to gui input any more! This cannot be anything else than a bug in PB as the command ReadProgramString() (which waits and locks up the gui if the stream is empty) should ONLY be called when there is data in the stream. There is no other command in my program that would lock up the gui as far as I can tell.
Well, I wait a bit longer. Now another bunch of numbers dumps into the listview (ca 30.) Then I go away to write this. When I get back to my program it is still locked up for a while. Then it reacts normally and the listview has now 145 items. I then write here again, now the program won't react again. Now it reacts. Now I got a bunch of new numbers and it stopped reacting.
Edit: I stopped the program with the debugger's kill command (it didn't react again) and piper.exe crashed with an unknown exception.
(The exception Unknown program exception (0x0eedfade) happened in the program at 0x7952bc3f.)---------------------------
piper.exe - Programfeil
---------------------------
Unntaket Ukjent programunntak (0x0eedfade) oppstod i programmet på 0x7952bc3f.
---------------------------
OK
---------------------------
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
The ReadProgramString() waits until a newline is found before it returns (or at program end for the last line),
so if the program outputs text but no newline, AvailableProgramOutput() will
return nonzero, but ReadProgramString() will not return immediately.
It would be nice if you could provide the delphi code as executable so i can test
if the newline detection works properly, or if the output shows the above problem.
The reason that the command is line-based is for ease of use. Most console
output is line-based, so it makes more sense to read it in that manner as well.
To prevent this, ReadProgramData() can be used.
so if the program outputs text but no newline, AvailableProgramOutput() will
return nonzero, but ReadProgramString() will not return immediately.
It would be nice if you could provide the delphi code as executable so i can test
if the newline detection works properly, or if the output shows the above problem.
The reason that the command is line-based is for ease of use. Most console
output is line-based, so it makes more sense to read it in that manner as well.
To prevent this, ReadProgramData() can be used.
quidquid Latine dictum sit altum videtur
You can test using this PB code:Be sure to compile as console application to piper.exe.
Test program:The PB program never receives anything.
Here is the compiled version of the delphi source above: http://d.turboupload.com/d/557577/Project2.exe.html
Code: Select all
Handle = GetStdHandle_(#STD_OUTPUT_HANDLE)
Repeat
Delay(500)
Str.s = Str(I) + #CRLF$
WriteConsole_(Handle, @Str, Len(Str), 0, 0)
I + 1
Until 1 = 0
Test program:
Code: Select all
OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ListViewGadget(0, 10, 10, 492, 364)
Pid = RunProgram("piper.exe", "", "", #PB_Program_Open | #PB_Program_Read)
Repeat
Select WaitWindowEvent(10)
Case #PB_Event_CloseWindow
Break
EndSelect
If AvailableProgramOutput(Pid)
String.s = ReadProgramString(Pid)
AddGadgetItem(0, 0, String)
EndIf
ForEver
KillProgram(Pid)
CloseProgram(Pid)
Here is the compiled version of the delphi source above: http://d.turboupload.com/d/557577/Project2.exe.html
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
http://www.penguinbyte.com/apps/pbwebst ... /PIPER.zipfreak wrote:It would be nice if you could provide the delphi code as executable so i can test
if the newline detection works properly, or if the output shows the above problem.
You can use that program made in QuickBasic 4.5.
Pure MS-DOS.
EDIT: Note that always when ProgramRunning(Program) is <>0 , AvailableProgramOutput(Program) is =0 , and viceversa. This means that one of those both functions is a nonsense.
Last edited by Psychophanta on Sat Apr 29, 2006 10:31 pm, edited 2 times in total.
1. Hmm, what does the Ln in WriteLn() mean?freak wrote:The reason that the command is line-based is for ease of use. Most console
output is line-based, so it makes more sense to read it in that manner as well.
To prevent this, ReadProgramData() can be used.
2. ReadProgramData() does not fix the problem.
I tried with the delphi version and out.txt is not empty.
Edit: I also tested PB's quickbasic version and out.txt is not empty.
Edit: Also I find it not right that we should have to know about pipes and stuff when the command description in the help file is "Read the programs console output" and WriteConsole_() is the correct way to write to the console as far as I can tell.
Edit: I also tested PB's quickbasic version and out.txt is not empty.
Edit: Also I find it not right that we should have to know about pipes and stuff when the command description in the help file is "Read the programs console output" and WriteConsole_() is the correct way to write to the console as far as I can tell.
Could you put the exe for download ? Edit: just seen in the above post. Edit2: here the redirected file is empty.Trond wrote:I tried with the delphi version and out.txt is not empty.
Edit: I also tested PB's quickbasic version and out.txt is not empty.
Edit: Also I find it not right that we should have to know about pipes and stuff when the command description in the help file is "Read the programs console output" and WriteConsole_() is the correct way to write to the console as far as I can tell.
About the WriteConsole_(), it doesn't work with pipes, period. You have to use WriteFile_() for this.