Recent

Author Topic: [Solved] Linux application freezes  (Read 3252 times)

nikel

  • Full Member
  • ***
  • Posts: 186
[Solved] Linux application freezes
« on: October 02, 2021, 01:19:00 am »
Hello, I created a multi threaded procedure, my buttons and grid is active. But when I press right top X the program can't close. Here's my code:

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.NormalizeAudio(Idx: Integer);
  2. Var
  3.   ...
  4. Begin
  5.   ...
  6.  
  7.   AProcess := TProcess.Create(Nil);
  8.   Try
  9.     AProcess.InheritHandles := False;
  10.     AProcess.Options := [];
  11.     AProcess.ShowWindow := swoShow;
  12.  
  13.     // Copy default environment variables including DISPLAY variable for GUI application to work
  14.     For I := 1 to GetEnvironmentVariableCount Do
  15.       AProcess.Environment.Add(GetEnvironmentString(I));
  16.  
  17.     AProcess.Executable := '/bin/bash';
  18.     AProcess.Parameters.Add('-c');
  19.     AProcess.Parameters.Add('ffmpeg -y -i "' + SongPath^.OldPath + '" -filter:a loudnorm "' + SongPath^.TempSongPath + '"');
  20.     AProcess.Execute;
  21.   Finally
  22.     AProcess.Free;
  23.   End;
  24.  
  25.   For Num:=0 To 4294967295 Do
  26.   Begin
  27.     If (AProcess.Active = True) Then Sleep(10) Else Begin ConvertAudio(QueueHandler^.CurrentIndex); NormalizeAudioUnset End;
  28.     Application.ProcessMessages;
  29.   End;
  30. End;

I tried to add functionality to exit program by pressing Esc but it didn't work eighter.
« Last Edit: October 06, 2021, 12:05:57 am by nikel »

dbannon

  • Hero Member
  • *****
  • Posts: 2786
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Linux application freezes
« Reply #1 on: October 02, 2021, 04:23:15 am »
Hmm, you are creating one TProcess and using it to launch multiple processes each in its own thread ?

Are you sure that TProcess is thread safe ?  And if it is, my  guess is it would be only if you created the TProcess in its thread. Still guessing, is it safe to free that TProcess before all threads have finished ?

Disclaimer - I have not tried any thing like what you are doing.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Linux application freezes
« Reply #2 on: October 02, 2021, 05:52:27 pm »
I know it's not a proper way of running external program in a queue. Once it run the queue alltogether. I think I have only 2 choice: To use a loop or call a function again and again. Am I right?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Linux application freezes
« Reply #3 on: October 03, 2021, 01:38:15 am »
The form can only close after the main eventloop is called again through the event handler returns.

Easy way to implement: add a variable/field Closed or similar and set it in the OnClose event handler to true. Then in your application.processmessages loop add another condition checking that the close wasnt called (by checking closed for false) and if that is the case exit the procedure. This will return to the main event loop and close the form

PS wu do you count from 0 to maxint rather than having a While True loop?

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Linux application freezes
« Reply #4 on: October 03, 2021, 11:01:05 pm »
Thanks  for the reply. I'm going to try that.

I didn't know about the "MaxInt".

nikel

  • Full Member
  • ***
  • Posts: 186
Re: Linux application freezes
« Reply #5 on: October 06, 2021, 12:05:37 am »
This worked fine:

Code: Pascal  [Select][+][-]
  1. For Num:=0 To 4294967295 Do
  2. Begin
  3.   If (AProcess.Active = True) Then Sleep(10) Else Begin ConvertAudio(QueueHandler^.CurrentIndex); NormalizeAudioUnset End;
  4.   If (CloseRequest = True) Then Begin Break End;
  5.   Application.ProcessMessages;
  6. End;
  7. If (CloseRequest = True) Then Begin NormalizeAudioUnset; Close End;

Thank you for your help.

 

TinyPortal © 2005-2018