How to ESCAPE a long running FOR-NEXT ?

  • Thread starter Thread starter Peter Stojkovic
  • Start date Start date
P

Peter Stojkovic

I want EXIT a FOR-NEXT construct via EXIT
if the user presses key ESCAPE

How can I check, whether the user presses ESCAPE
while my program is in the FOR-NEXT construct ??


Peter
 
Hi Peter,

In a windows program you could add a flag (boolean value) that you set on
the Escape key in a KeyPress/Down/Up event.

In your loop, every so often do Application.DoEvents() and then check to
see if the flag is true, if so, break the loop.
 
Like this:

class MyForm:Form
{
private mStop=false;
...
//You can change following Click event handler to KeyPress/Down/Up to
capture "ESC" key storke
private btnStop_Click(object sender, Systen,EventHandler e)
{
mStop=true;
}

....

private void DoLengthyLoop()
{
for (i=0; i<1000000; i++)
{
////Do something
Application.DoEvents() ////Or if (i%10==0)
Application.DoEvents(), i.e. every 10 loops, check Stop flag.
if (mStop) break;
}
}

....
}
 
Back
Top