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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaking out of a loop with a key press 2

Status
Not open for further replies.

ChrisGSchultz

IS-IT--Management
Jul 8, 2001
82
US
Hi folks!

I'm trying to break out of a loop with a keypress, and have tried all sort of things, including the GetKeyState function included below, and a global hot key - but to no avail!

The Database I'm searching is very large (200,00 patients, > 1,000,000 images, and >> 1,000,000,000 result records), and whilst I simplify with a query to get the results, there is still a need to break out.

Here is the loop itself:
with ADOTable_Patients do begin
First;
ScanBreak := FALSE;
Application.ProcessMessages;
try
B_DataExtraction.Visible := FALSE;
CurrCursor := Screen.Cursor;
Screen.Cursor := crHourGlass; { hourglass cursor }
while not ADOTable_Patients.Eof do begin
if GetKeyState(VK_ESCAPE) and 128 = 128 then begin
ScanBreak := True;
Exit;
end; // if
aScanRecord := ExtractData; { this extracts images and results records}
next;
LMD_PFill.UserValue := ADOTable_Patients.RecNo; {a simple meter}
end; // while not eof
finally
B_DataExtraction.Visible := TRUE;
Screen.Cursor := CurrCursor; { restore cursor }
end;
end; // with ADOTable_Patients


Any suggestions greatly appreciated!

and BTW - Happy New Year to all!




Chris ;-)
 
Have you tried putting the Application.ProcessMessages inside the loop such as:
Code:
 while not ADOTable_Patients.Eof do begin  
  Application.ProcessMessages;
  if GetKeyState(VK_ESCAPE) and 128 = 128 then begin
   ScanBreak := True;
   Exit;
  end; // if
  aScanRecord := ExtractData; { this extracts images and results records}
  next;
  LMD_PFill.UserValue := ADOTable_Patients.RecNo; {a simple meter}
 end; // while not eof

Incidentally, try to use the correct TGML for code as it makes it easier for us oldies to read.

Andrew
 
you can do this very easy :

create a global variable like this

var BreakLoop : boolean;

make sure that your form.KeyPreview is set to True;

assign an onKeydown event to your form :

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
BreakLoop:=Key = VK_ESCAPE;
end;


then change your routine :

with ADOTable_Patients do begin
First;
BreakLoop := FALSE;
try
B_DataExtraction.Visible := FALSE;
CurrCursor := Screen.Cursor;
Screen.Cursor := crHourGlass; { hourglass cursor }
while not ADOTable_Patients.Eof do begin
Application.ProcessMessages;
if BreakLoop then Break; // or Exit, whatever you like
aScanRecord := ExtractData; { this extracts images and results records}
next;
LMD_PFill.UserValue := ADOTable_Patients.RecNo; {a simple meter}
end; // while not eof
finally
B_DataExtraction.Visible := TRUE;
Screen.Cursor := CurrCursor; { restore cursor }
end;
end; // with ADOTable_Patients

greetings,

 
hi

I'd be tempted to keep the loop with a 'tidy' exit so it finishes the current cycle before exiting. (I've also changed the position of the Application.processmessages). eg

:
Application.ProcessMessages;
while not ADOTable_Patients.Eof and not BreakLoop do
begin
aScanRecord := ExtractData; { this extracts images and results records}
next;
LMD_PFill.UserValue := ADOTable_Patients.RecNo; {a simple meter}
Application.ProcessMessages;
end; // while not eof
:

lou


 
Thanks to all - put the Application.ProcessMessages in the loop and it worked perfectly.

I had tried the FormKeyDown approach earlier, and that hadn't worked. But the Application.ProcessMessages procedure wasn't in the right place there either.

Much appreciated!

BTW, apos for the wrong TGML tag

Cheers


Chris ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top