Recent

Author Topic: TAsyncProcess doesn't call OnTerminate?  (Read 1788 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
TAsyncProcess doesn't call OnTerminate?
« on: April 21, 2019, 03:22:43 pm »
Environment:
* Lazarus 2.0.2/FPC 3.0.4
* Ubuntu 12.04

Problem:

I have a small GUI program which processes a series of files by calling a external program with TAsyncProcess.

I'm using TAsyncProcess specifically because it has the OnTerminate event but it never gets called!.

The processing starts on a button click:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.btPlayAllClick(Sender: TObject);
  2. begin
  3.   btPlayAll.Enabled := False;
  4.   PlayList := True;
  5.   Current := -1;
  6.   btNext.Click;
  7. end;

The OnClick event for btNext (another button) does some housekeeping and starts playing one song:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.btNextClick(Sender: TObject);
  2. var
  3.   Max: Integer;
  4. begin
  5.   Max := FileList.Count - 1;
  6.   if (Max > 0) then begin
  7.     if Current < Max then
  8.       PlaySong(Current + 1)
  9.     else
  10.       PlaySong(0);
  11.   end;
  12. end;

PlaySong gets just setups the parameters for TAsyncProcess and Executes it. That part works ok but ...when the AsyncProcess terminates it should call the handler for its OnTerminate event which just happens to advance to the next file and play it:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.ProcessTerminate(Sender: TObject);
  2. { When in "playlist" mode it should either play the next file
  3.   or stop if it's the last, but ... it never gets called! }
  4. var
  5.   Max: Integer;
  6. begin
  7. {@test}ShowMessage('Process terminated'); {<-- Never appears!!!}
  8.   StatusBar.SimpleText := sStopped;
  9.   Max := FileList.Count - 1;
  10.   if PlayList then
  11.     if (Current < Max) then
  12.       PlaySong(Current+1);
  13.     else begin
  14.       PlayList := False;
  15.       btPlayAll.Enabled := True;
  16.     end;
  17. end;

As it says, it never gets called , so clicking the "Play all" button just plays the first file.

And no, I didn't forget to set the OnTerminate event. :)

Any help?

In case it matters, I'm attaching the whole project. It's quite crude, just a quick first iteration to test the concept. It'll be trashed after the redesign once it has served its purpose, so don't be too critical with it. ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: TAsyncProcess doesn't call OnTerminate?
« Reply #1 on: April 21, 2019, 04:01:09 pm »
are you using the WaitOnExit ? I don't see any other way how the Onterminate is going to get fired automatically.

So even if you directly call the Terminate method, that should also call the OnTerminate when the program  exits.

But waiting for it to talk back to you may be futile!

Just my opinion
 :D
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: TAsyncProcess doesn't call OnTerminate?
« Reply #2 on: April 21, 2019, 04:42:02 pm »
Put it in a Thread and use the Wait option

 Read that,....
https://github.com/Basile-z/dexed/issues/337
« Last Edit: April 21, 2019, 04:44:43 pm by jamie »
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TAsyncProcess doesn't call OnTerminate?
« Reply #3 on: April 21, 2019, 05:57:52 pm »
are you using the WaitOnExit ? I don't see any other way how the Onterminate is going to get fired automatically.

So even if you directly call the Terminate method, that should also call the OnTerminate when the program  exits.

But waiting for it to talk back to you may be futile!

Just my opinion
 :D

There is nothing whatever either in the documentation or the code (as far as I understood it) that suggest anything of the sort. Nor shoud it be: why would you expect a call to OnTerminate after WaitOnExit? If you're sitting there waiting for it to exit you don't need OnTerminate to be shot--you're already there. The only reason to have an event is so you can do other things while the process does its own.

As for Terminate: sorry, it doesn't call it ... probably for the same reason: If you're forcibly terminating the process, your next lines will do whatever they need knowing the process is shot down (and dead) :D

And I'm definetely not waiting to be called back: I'm attending the user and (in the next versions) lots of other funny things. That's why I selected TAsyncProcess: because it has this couple of events I was hoping it would notify me when it wanted attention. That's what events are for.

Put it in a Thread and use the Wait option
 Read that,....
https://github.com/Basile-z/dexed/issues/337

Hmm ... I was really hoping it had an (even relatively) easy solution. Threads and Wait are really not solution at all but I'll think a little about it. BTW, the relevant link is to the bugtracker issue #17807 ... which has been open since 2010.

It's a pitty: OnTerminate would simplify a lot the logic of this program (and others I have in mind). Without it TAsyncProcess is practically like TProcess.


I'lll wait a little to see if someone knows something more and, if not, I'll close the thread as unresolved. :'(
« Last Edit: April 21, 2019, 06:00:11 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: TAsyncProcess doesn't call OnTerminate?
« Reply #4 on: April 21, 2019, 07:05:46 pm »
Specifics of Ubuntu? On Windows, this code works without problems and the ProcessTerminate is called.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TAsyncProcess doesn't call OnTerminate?
« Reply #5 on: April 21, 2019, 08:12:10 pm »
What specifics do you want?

Here is uname -a, for example:
Code: [Select]
Linux selene 3.2.0-126-generic #169-Ubuntu SMP Fri Mar 31 14:16:01 UTC 2017 i686 i686 i386 GNU/Linux
Oh, btw, I'm running a GNOME falback session instead of Unity(-2D)
« Last Edit: April 21, 2019, 08:13:50 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018