[Lazarus] How to use RunCommandIndir() in a thread?

Bo Berglund bo.berglund at gmail.com
Mon Apr 15 14:49:49 CEST 2019


On Mon, 15 Apr 2019 00:59:16 +0200, Bo Berglund via lazarus
<lazarus at lists.lazarus-ide.org> wrote:

>I havr written a wrapper for some ffmpeg functions to make them easier
>to work with.
>However, some of the functions are rather longish so the GUI
>application gets unresponsive...
>I would like to fix this perhaps by running the commands inside a
>thread, but I don't really know how that can be done...
>RunCommandIndir() is a command line interface I use to start ffmpeg
>with selected arguments.
>I would like to implement some solution that puts the
>RunCommandIndir() call into a thread so the main application could for
>instance show a running clock and have a button to interrupt the task
>if it turns out to be too long.
>Any suggestions on how to do this?`
>Or is there already a threaded version of RunCommandIndir() somewhere?
>
>I use Fpc 3.0.4 and Lazarus 2.0.0 on Windows 7 and Ubuntu Linux to
>compile for both environments.
>

I found a webpage describing basically the same as I am looking for:
https://www.sigmdel.ca/michel/program/fpl/yweather/process_thread_en.html
However this uses a TProcess object and I don't think that the call I
am making (RunCommandIndir) will work in the Execute loop...

He has the following in Execute():

...
var PlayProc: TProcess; 
begin PlayProc := TProcess.create(nil);
  try
    PlayProc.executable := 'aplay';
    PlayProc.parameters.add(FFilename);
    PlayProc.Options := [poNoConsole];
    PlayProc.execute; // <== Will this really return before done?
    while PlayProc.Running do  // If not then we will not get here....
    begin
      if StopPlay or AppClosing then
      begin
        if StopPlay or FTerminateProcess then
          PlayProc.terminate(1);
        exit;
      end
      else
        sleep(1);
    end;
  finally
....

Seems like if one places a blocking call to a lengthy process in
Execute() then one loses the ability to interrupt the running process,
right?

So how could it be done instead?

-- 
Bo Berglund
Developer in Sweden



More information about the lazarus mailing list