//如果有这样一个循环, 是非常可怕的; 因为它完不了, 你得等着. procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin for i := 0 to MaxInt do begin Text := IntToStr(i); end; end;
//即使这样也无济于事, 因为在循环期间你执行不了 Button2Click var b: Boolean; procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin b := True; for i := 0 to MaxInt do begin if b then Text := IntToStr(i) else Exit; end; end; procedure TForm1.Button2Click(Sender: TObject); begin b := False; end;
//如果在循环期间... for i := 0 to MaxInt do begin if b then Text := IntToStr(i) else Exit; {看看还有其他什么事情发生} end;
//Application.ProcessMessages 就是干这个的! var b: Boolean; procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin b := True; for i := 0 to MaxInt do begin if b then Text := IntToStr(i) else Exit; Application.ProcessMessages; {!} end; end; procedure TForm1.Button2Click(Sender: TObject); begin b := False; end;