Quebrar laço do while - tá difícil

Delphi

27/02/2004

Galera, tenho a seguinte rotina no meu aplicativo (Delphi 5 e paradox):

tabela1.First;
while not tabela1.eof do
begin
tabela2.First;
while not tabela2.eof do
begin
tabela3.First;
while not tabela3.eof do
begin
..........
comandos para gravar na tabela
..........
application.processmessages;
end;
end;
end;

O que eu gostaria é: se eu desistir de esperar a rotina dos laços acabarem, parar o laço clicando em um botão de cancelar.
Como eu faço isso ? Já tentei um monte de coisa e nada.


Edukobra

Edukobra

Curtidas 0

Respostas

Beppe

Beppe

27/02/2004

Coloca uma variável no public:
PrecisaParar: Boolean;


O seu código fica:
label Fim;
begin
PrecisaParar := False;

tabela1.First; 
while not tabela1.eof do 
begin 
tabela2.First; 
while not tabela2.eof do 
begin 
tabela3.First; 
while not tabela3.eof do 
begin 
.......... 
comandos para gravar na tabela 
.......... 
application.processmessages; 
if PrecisaParar then goto Fim;
end; 
Fim:


Como há loops aninhados, usar goto é mais fácil, senão seria:
PrecisaParar := False;

tabela1.First; 
while not PrecisaParar and not tabela1.eof do 
begin 
tabela2.First; 
while not PrecisaParar and not tabela2.eof do 
begin 
tabela3.First; 
while not PrecisaParar and not tabela3.eof do 
begin 
.......... 
comandos para gravar na tabela 
.......... 
application.processmessages; 
end; 
Fim:


No click do Cancelar, coloca PrecisaParar := True;


GOSTEI 0
POSTAR