Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying whilst looping? 2

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I have this simple routine which deletes a list of files contained in Memo1 that was previously built.
This works fine, except I want to show some form of progress.

As you can see I have a line to update a label caption in the loop, but that does not update until the end of the loop. The list can be pretty long, so it has the effect of hanging until finished.

Can someone suggest a better method please.

Code:
procedure TSDIAppForm.Button3Click(Sender: TObject);
Var
  Sure, Xpos, Ypos, count: integer;
  DeleteFileName: string;
begin
  Xpos := (Self{SDIAppForm}.Width Div 2) + Self{SDIAppForm}.Left - 120; //-120 to centre msgbox hor on form
  Ypos := (Self{SDIAppForm}.Height Div 2) + Self{SDIAppForm}.Top - 50;  //-50 to centre msgbox vert on form
  // Show a confirmation dialog
  Sure := MessageDlgPos('Are you certain you are REALLY REALLY sure?',mtCustom, [mbYes,mbCancel], 0, Xpos, Ypos);
  if Sure = mrYes then
    begin
      for count := 0 to Memo1.Lines.Count-1 do
        begin
          DeleteFileName := Memo1.Lines[count];
          Deleting.Caption := DeleteFileName;
          DeleteFile(DeleteFileName);
        end;
      Memo1.Lines.Clear;
    end;
end;


Steve (Delphi 2007 & XP)
 
You can throw in:
Application.ProcessMessages;

That will surrending control to the OS so it can handle any messages that have arisen while you had control. Once those messages have been dealt with control is passed back to you.

If you want to make sure that you're getting more CPU time you can reduce the number of times that you're calling ProcessMessages:

if count mod 10 = 0 then
Application.ProcessMessages;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top