var
Flag:Boolean;
procedure TForm1.StopButtonclick(sender:Tobject);
begin
Flag:=True;
end;
procedure TForm1.DoButtonClick(sender:Tobject);
var
i:integer;
begin
for i:=0 to 20000000do
begin
//do something
Application.Processmessage;
if Flag then
Break;
//or Exit;
end
end;