0

I have a Startandwait function that creates a process and wait for the end. How do I create a progress bar to indicate the progress of the process?

function StartRAndWait (CommandLine : string) : Boolean;

var
  Proc_info: TProcessInformation;
  Startinfo: TStartupInfo;
  ExitCode: longword;
  CreateOK : Boolean;

begin

Result := False;

  FillChar(proc_info, sizeof (TProcessInformation), #0);
  FillChar(startinfo, sizeof (TStartupInfo), #0);
  Startinfo.cb := sizeof (TStartupInfo);
  Startinfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  Startinfo.wShowWindow := SW_HIDE;

  CreateOK := CreateProcess(Nil, PChar('Program.exe ' + 'CMD  BATCH  ARQ.EXT  SampleOutput.txt'), nil,
  nil,False, CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS, nil,nil, StartInfo, proc_info);

  if (CreateOK) then
  begin
  WaitForSingleObject (proc_info.hProcess, INFINITE);
  GetExitCodeProcess(proc_info.hProcess, ExitCode);
  Result := True
  end;
  CloseHandle(proc_info.hThread);
  CloseHandle(proc_info.hProcess);

end;
Artur_Indio
  • 736
  • 18
  • 35
  • What progress do you want to display exactly? A process does not have a progress state of its own. If you are trying to display progress of something that the spawned process is doing, the spawned process will have to communicate that information back to your main process through an IPC mechanism, such as a pipe. – Remy Lebeau Aug 08 '14 at 18:25
  • Marquee progress bar is what you need unless you can use ipc to get meaningful progress measurements. – David Heffernan Aug 08 '14 at 18:27
  • Why are you calling CloseHandle when CreateProces fails? – David Heffernan Aug 08 '14 at 19:10
  • @DavidHeffernan true I will fix it. – Artur_Indio Aug 08 '14 at 19:15
  • I think I expressed myself badly, I actually wanted the progress bar was filling according to the time that the process goes. So when he finishes the bar fills up quickly. She need not show exactly the progress of the process itself, just show the User that has something going on, not just the mouse pointer. – Artur_Indio Aug 08 '14 at 19:17
  • Use a marquee progress bar – David Heffernan Aug 08 '14 at 19:21
  • It's a good alternative. – Artur_Indio Aug 08 '14 at 19:27
  • @Artur it's not an alternative. If you can't predict when the task will end it's your only good choice. – David Heffernan Aug 08 '14 at 19:30
  • and this IPC mechanism? I think it is a more robust alternative, but I will study it yet. – Artur_Indio Aug 08 '14 at 19:41
  • If you control both processes you can use ipc to communicate progress. You indicated in comments that you cannot. I suspect the other process is R. What progress can you expect from that. Perhaps sucking in the standard output combined with a marquee would do. – David Heffernan Aug 08 '14 at 19:44
  • Now clarified my mind, thanks! Yes It's R of course! My last questions are about R and Delphi! I will try marquee. – Artur_Indio Aug 08 '14 at 19:54

2 Answers2

1

"The progress of the process" has no inherent meaning in and of itself. A progress bar displays exactly what it is programmed to display, which may or may not have anything to do with realistically representing the progress of anything taking place in the program. So first you need to define what the "progress of the process" means in this specific context. But let's assume you've already done that.

The second step is to have your process communicate its progress back to the calling process. Do you own the second program? Can you make it write to standard output (writeln)? If so, have a look at the second example listed in this answer for a way to create a process and make it read the process's output. Then you just need the child process and the parent process to both understand the format of the output it writes, and update the progress bar accordingly every time the child gives an update.

If not, does the child process already output its progress in some way? Then the same principle applies, you just need to adopt the format it uses. If not, then you're kind of out of luck unless you can get the author to add this output as a feature.

Community
  • 1
  • 1
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • I edited my code. I have an output that I'm putting in a txt file. Please take a look at my comment above. But I think coma answer (example) that you quoted from to solve my problem. – Artur_Indio Aug 08 '14 at 19:22
0

Judging from the comments you are looking for a marquee progress bar. Set the progress bar's Style property to pbstMarquee. A marquee progress shows progress by continuously sliding the indicator back and forth.

You'll need to pump the message queue to keep the progress bar animation alive. Perhaps run the CreateProcess code in a separate thread and signal the main thread when it is done. Or use a MsgWaitForMultipleObjects based wait that allows you to wait and service the queue.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490