Delphi example of using TTaskDIalog for progress indication

6.3k views Asked by At

I can not find an example of using TTaskDialog in Delphi to show a progress bar. The Embarcadero docs are not helpful at all as far as TTaskDialog is concerned.

The best guide I found:

http://specials.rejbrand.se/TTaskDialog

doesn't contain any sample for showing a progress bar.

I can see various flags in the Delphi source for progress bar but in order to try them out, I have no idea how the task dialog can be displayed modeless to experiment with the flags.

Update: I have come to the conclusion that the task dialog can not be used in the traditional way that modeless progress dialogs are used. Here is what I usually do for long running operations:

show progress dialog modeless
start a loop to do work
  ... update progress bar in above dialog (often on a modulo count)
  ... check for cancel and abort if needed
remove progress dialog

My experiments with TTaskDialog based on an answer by bummi show the following:

  • Timer event does not help for updates to the progress bar. The event gets fired but any updates to the progress bar in the timer event do not show up even with updatewindow call.
  • The dialog can not be started modeless so even if the timer event is somehow made to update the progressbar, the logic has to change considerably to proceed with work in the timer event.
  • The only way progress bar position can be shown is to set it before execute. In that sense, it works exactly as described in the answer by SilverWarior. Its likely use seems to be, show in a looping operation with a new progressbar position, only when you need to get the next button response from the user. So that seems to be the correct answer but I will wait for more responses to this update.

P.S. I used Delphi 2007 for this test. So I don't know if the progress bar updates from timer work for a later IDE. But I doubt it because even the D2007 code internally sends standard TaskDialog message to update the progress bar.

1

There are 1 answers

3
bummi On

If you add tfCallbackTimer to the Flags the OnTimer- Event will be triggered 5 times per second.
Since the dialog is blocking a use case could be having a thread copying files with a tread save property for the progress.
Within the timer you are able to reflect the current progress.

enter image description here

begin
  TaskDialog1.ProgressBar.Min := 0;
  TaskDialog1.ProgressBar.Max := 100;
  TaskDialog1.Execute;
end;

procedure TMyForm.TaskDialog1Timer(Sender: TObject; TickCount: Cardinal; var Reset: Boolean);
begin
   // TaskDialog1.ProgressBar.Position := MyThread.CurrentProgressPercent;
   // Demo
   TaskDialog1.ProgressBar.Position :=  TaskDialog1.ProgressBar.Position + 1;
end;