Recent

Author Topic: External Process ReadOutput stucks  (Read 2227 times)

GIOhnny

  • Newbie
  • Posts: 5
External Process ReadOutput stucks
« on: April 20, 2015, 04:44:46 pm »
Hello,

When I'm executing an external process using TProcess the command
NumBytes := aProcess.Output.Read((MemStream.Memory + BytesRead)^,READ_BYTES);
waits forever, and this because the external program waits for some input.
How can I force the external application run after 10 sec ?

Please help me. I've searched for a solution for 3 days now.

Code: [Select]
procedure RunExternalAppInMemo(DosApp:String;AMemo:TMemo);
  const
    READ_BYTES = 2048;
  var
    aProcess: TProcess;
    MemStream: TMemoryStream;
    NumBytes: LongInt;
    BytesRead: LongInt;
    Lines: TStringList;

  begin
   // A temp Memorystream is used to buffer the output
   MemStream := TMemoryStream.Create;
   Lines :=TStringList.Create;
   BytesRead := 0;

     aProcess := TProcess.Create(nil);
     aProcess.CommandLine := DosApp;
     aprocess.ShowWindow := swoHIDE;
     AProcess.Options := [poNoConsole, poUsePipes, poStderrToOutPut] ;
     aProcess.Execute;

     AMemo.lines.AddStrings(AProcess.CommandLine);

     while (aProcess.Running) do
     begin
       // make sure we have room
         MemStream.SetSize(BytesRead + READ_BYTES);
         NumBytes := aProcess.Output.Read((MemStream.Memory + BytesRead)^,READ_BYTES);
           if NumBytes > 0 then// All read() calls will block, except the final one.
               Inc(BytesRead, NumBytes)
           else sleep(100);
     end;
     MemStream.SetSize(BytesRead);
     Lines.LoadFromStream(MemStream);
     AMemo.lines.AddStrings(Lines);
     aProcess.Free;
     Lines.Free;
     MemStream.Free;
  end;   

Thank you

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: External Process ReadOutput stucks
« Reply #1 on: April 20, 2015, 06:59:58 pm »
and this because the external program waits for some input.
...
How can I force the external application run after 10 sec ?
if the external program waits for input, shouldn't you be giving the expected input instead of forcing it to run after 10 sec (not really sure with this run after 10 sec, you might mean run for only 10 secs or something else)?

Basile B.

  • Guest
Re: External Process ReadOutput stucks
« Reply #2 on: April 20, 2015, 07:13:52 pm »
A little bit more in direction of the previous answer:

if the output content is displayed in a memo, then the end-user will able to see that the process expect an input. Maybe you could add an input field so that the user can send what's the process is expecting.

If you do so, one important detail: your TProcess must not be a local variable !!

GIOhnny

  • Newbie
  • Posts: 5
Re: External Process ReadOutput stucks
« Reply #3 on: April 20, 2015, 11:05:02 pm »
So the external command I'm running is :
adb -s XXXX install -r "Program.apk" (this command installs the apk directly on the Android device)

If the device XXXX was disconnected meanwhile the adb program has the following message and he waits to be reconnected.

error: device not found
- waiting for device -

There is no effective input but the reconnection of the device.  ;)

What I need is exactly what Leledumbo said: run for only 10 secs. If no reconnection then kill the process.

Thank you
« Last Edit: April 20, 2015, 11:13:17 pm by GIOhnny »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: External Process ReadOutput stucks
« Reply #4 on: April 21, 2015, 10:33:24 am »
You could try to create a TProcess derivative that tries to use reads that timeout after a while, so that control returns.

Maybe it can't be done crossplatform, but afaik nobody has tried either, so who knows?

 

TinyPortal © 2005-2018