Search code examples
freepascallazarus

How do I put delay/sleep between executing two external programs using pascal?


I have code like:

begin
    RunProgram:=TProcess.Create(nil);
    RunProgram.Commandline:='calc.exe';
    RunProgram.Execute;
    RunProgram.Commandline:='notepad.exe';
    RunProgram.Execute;
    RunProgram.Free;
end.

and I would like to put a sleep or delay after executing calc.exe


Solution

  • You had the right idea - it's Sleep.

    begin
      RunProgram:=TProcess.Create(nil);
      RunProgram.Commandline:='calc.exe';
      RunProgram.Execute;
      Sleep(1000);             // Adds a 1 second delay
      RunProgram.Commandline:='notepad.exe';
      RunProgram.Execute;
      RunProgram.Free;
    end.
    

    You may need to add the Windows unit (or possibly a different one - I'm not familiar with FreePascal's unit arrangement) to your uses clause to be able to compile the Sleep function.