Recent

Author Topic: [Solved]TAsyncProcess.OnTerminate won't fire after external command is completed  (Read 2408 times)

FangQ

  • Full Member
  • ***
  • Posts: 134
Sorry for reviving a 6-yr old topic, here is a previous thread in the lazarus mailing list

http://lazarus.lazarus.freepascal.narkive.com/dCiCiyW2/tasyncprocess-onterminate-doesn-t-fire

I am having exactly the same issue. I run a command using TAsyncProcess, and prints the output in a TMemo as it executes. When the command completes, I set the status of some buttons.

I had trouble 8 years ago on capturing the outputs on-the-fly, but it was solved by doing a flush in the external command, here is my code

http://forum.lazarus.freepascal.org/index.php?topic=8111.0

however, since then, I have noticed the issue that, even the external command is completed, the OnTerminate event never gets fired. I can set the poWaitOnExit option to make things to work, but that defeats the purpose of using TAsyncProcess.

how can I detect that a command has returned in TAsyncProcess?

I can share my sample program if anyone is interested. appreciated for any comment.
« Last Edit: June 22, 2017, 03:24:32 pm by FangQ »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Not quite what you ask for, but maybe you would like to take a look at RunProcess() here:
https://bitbucket.org/avra/ct2laz/src/5b5b18f23095153688df444fffe66629471063f4/utils.pas?at=master&fileviewer=file-view-default
It is a simple cross platform way to execute command line executable and live capture it's output. It might, or it might not help. Only you can tell...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

FangQ

  • Full Member
  • ***
  • Posts: 134
@avra, thanks for the reply. I ended up solving the problem by inserting a Sleep(100) after each output reading function. This way, I could test for TAsyncProcess.Running flag.

if I do not give it a short break, after the available bytes are all read and the program terminates, the Running flag may not be updated soon enough.

 

TinyPortal © 2005-2018