Recent

Author Topic: Redirect program-output to TMemo in real-time  (Read 37816 times)

Lazarus

  • New Member
  • *
  • Posts: 26
Redirect program-output to TMemo in real-time
« on: June 20, 2012, 07:47:17 pm »
Hi,

I've a problem: I start a Minecraft-Server via LCL's TProcess and I want to read the output.
That's no problem, but I would like to redirect the output to a Memo in real-time ("on-the-fly")!

The output is big!

I've already seen this thread, but it gives me only question marks (?) as output in my memo.

That's what I've already have:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  Process: TProcess;
begin
  Process := TProcess.Create(nil);
  Process.CommandLine := 'java -Xmx1024M -Xms1024M ' + OpenDialog1.FileName + ' nogui';
  Process.Options := [poUsePipes];
 
  Process.Execute;
 
  while Process.Running do
  Memo1.Lines.LoadFromStream(Process.Output);
end;

Thanks for replies,
Lazarus

PS: I started a new topic because I don't want to push old topics ;).

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Redirect program-output to TMemo in real-time
« Reply #1 on: June 20, 2012, 08:52:05 pm »
You need some sort of Event loop for grabbing the data from the OuputStream,  LoadFromStream will just block until it read 0 bytes.

I've created a quick example in windows, that does a "Dir c: /s" , this basically lists all files on your C drive, and then outputs the results to a TMemo.  I've just used a TTimer to poll the OutputStream.

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #2 on: June 21, 2012, 06:23:23 pm »
Thanks for this reply!
I'm very new in Lazarus (around 1 - 2 month).

I tried to recreate your program, but I didn't get it :(.
Do you mean with "eventloop" a repeat-loop?

Can you maybe give me your source :D?

Greetings,
Lazarus

Edit: Sorry, I haven't seen that you have added a attachment :o! I'm very new at this board, sorry...
Many thanks anyway! I'll test this now!

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #3 on: June 21, 2012, 06:31:08 pm »
That's exactly what I needed!!!
Many, many thanks!

I'll try to work with it and implement in my program, when I need some help, I'm going to ask you ;).

Many thanks again!

Greetings,
Lazarus

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #4 on: June 21, 2012, 08:36:57 pm »
The TForm1.Timer1Timer-Procedure in your program doesn't work for me because Process.Output isn't assigned yet. Is there a possiblity to start the procedure after the Process is started?

I've already tried to use a while and repeat-loop, but I cannot get it work :(.

Does anybody have an idea how to get this work?


But by the way: I really love your code! I've never found something easy-understanding like that before! I couldn't imagine it is so easy to solve my problem :).

Greetings,
Lazarus

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Redirect program-output to TMemo in real-time
« Reply #5 on: June 21, 2012, 11:20:49 pm »
Are you on windows?, my example was only windows only.

If you are on windows, could you double check the following command indeed works. -> 
Code: [Select]
c:\windows\system32\cmd.exe /c dir c:\ /s
You might have you system32 directory on a different drive etc.  It works for me on Windows 7 64bit, but I don't have UAC on either, not sure but maybe it could also be permissions, so maybe run as administrator.

Quote
Process.Output isn't assigned yet

That's why I have the following on the first line of the Timer event.
Code: [Select]
if not assigned(process.output) then exit; 

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #6 on: June 22, 2012, 02:28:03 pm »
I'm on Windows 7 32bit and I don't have any problems with your program :)! It works very fine on my computer.

My acutal problem is in my program written myself.
It gives me exactly at this line an Execpt-Class "External SIGSEGV" error at the beginning of the program (I start it and the error appears :o)
That's why I have the following on the first line of the Timer event.
Code: [Select]
if not assigned(process.output) then exit; 

That's very misterious because your program works very well and there's no error.


Thank you for helping me!

Greetings,
Lazarus

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Redirect program-output to TMemo in real-time
« Reply #7 on: June 22, 2012, 03:10:30 pm »
Quote
"External SIGSEGV" error at the beginning of the program

Could it be that Process is not yet defined?.
If you can Zip up your source and attach here, I could have a look.

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #8 on: June 22, 2012, 04:59:55 pm »
Thank you for your help!

I've attachted the SourceCode.
I would appreciate it, if you could find the mistake ;)


Greeting,
Lazarus

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Redirect program-output to TMemo in real-time
« Reply #9 on: June 22, 2012, 05:14:10 pm »
Your only creating the instance of Server on the StartServerClick event.

Create the Server / TProcess on the Forms OnCreate Event, also set self as the Owner, and it will free itself when the forms closes.

So, first remove the Server := TProcess.Create(nil) in your StartServerClick Event, and then add this to the OnFormCreateEvent.

Code: [Select]
procedure TMainWindow.FormCreate(Sender: TObject);
begin
  Server := TProcess.Create(self);
end;

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Redirect program-output to TMemo in real-time
« Reply #10 on: June 25, 2012, 10:18:22 am »
Or disable Timer until you click on StartServerClick button.

Or assign process=nil in FormCreate and add another check to Timer event like:
Code: [Select]
if (process = nil) then
  exit
else
if not assigned(process.output) then
  exit
else
begin
  // do what you need
end;

EDIT: Thanks KpjComp for correction
« Last Edit: July 03, 2012, 09:44:25 am by Shebuka »

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Redirect program-output to TMemo in real-time
« Reply #11 on: June 25, 2012, 11:26:43 am »
Code: [Select]
if (process = null) then

I assume you mean nil ?

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #12 on: July 02, 2012, 07:27:02 pm »
Thanks for your help, you both!

I just had some time again and so I've worked on a little bit on my project.

Unfortunately, I found a bug in the program.
As already said, I plan to redirect the Output of a Minecraft Server into a TMemo.

KpjComp's solution works very well for all programs I've just tested (thanks for that again :D!) but there's a error with the Minecraft Server. When I start the process, the server,  it redirects the first two lines of the output and then the program hangs up/freezes.

That's a pitty, because your solution works for all other programs :o!


Thanks in advance (again ;)) and many greetings,
Lazarus

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #13 on: July 06, 2012, 08:30:14 pm »
I made a new discovery:
The programm freezes while Minecraft Server is running in the background. While it freezes, of course, no output will be redirected.

But when I force to Minecraft Server to stop via TaskManager, my program doen't freeze anymore, but no output will be redirected :(

Maybe this helps...

Greetings,
Lazarus

PS: I even tried to change the Timer-Intervall but it makes no difference...

Lazarus

  • New Member
  • *
  • Posts: 26
Re: Redirect program-output to TMemo in real-time
« Reply #14 on: July 13, 2012, 01:37:20 pm »
Does really nobody have a solution for that problem?

If it helps, I could also add the Source!

Greetings in advance,
Lazarus

 

TinyPortal © 2005-2018