Hopefully this will provide some ideas as to how to adapt a timer based approach for your needs:
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Timer1: TTimer; // with Interval = 1 (for example) and Enabled = FALSE
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Execute(Sender: TObject);
private
fCounter: Integer;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := TRUE;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Timer1.Enabled := FALSE;
end;
procedure TForm1.Timer1Execute(Sender: TObject);
begin
Inc(fCounter);
end;
If your needs are more sophisticated then a TThread based approach may be more appropriate. But whatever you do, do NOT resort to Application.ProcessMessages it will only cause you grief !