loop break on KeyDown [modified]
-
please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :
private bool EXIT;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
EXIT = true;
}
}
private void Function()
{
for (int x = 0; x < Max; x++)
{
if(EXIT)
{
break;
}// work on image... OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight))); }
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage( image, 0, 0, Width, Height);
}when the Esc key is pressed nothing happens. what i have to do?
sometimes 0 can be 1
-
please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :
private bool EXIT;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
EXIT = true;
}
}
private void Function()
{
for (int x = 0; x < Max; x++)
{
if(EXIT)
{
break;
}// work on image... OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight))); }
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage( image, 0, 0, Width, Height);
}when the Esc key is pressed nothing happens. what i have to do?
sometimes 0 can be 1
i dont see anything wrong there... but thinking about it a little more, the for loop MIGHT be holding up the UI thread, which MIGHT mean that the
Form1_KeyDown
handler only gets called after the for loop is finished executing.Harvey Saayman - South Africa Junior Developer .Net, C#, SQL
you.suck = (you.passion != Programming)
-
please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :
private bool EXIT;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
EXIT = true;
}
}
private void Function()
{
for (int x = 0; x < Max; x++)
{
if(EXIT)
{
break;
}// work on image... OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight))); }
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage( image, 0, 0, Width, Height);
}when the Esc key is pressed nothing happens. what i have to do?
sometimes 0 can be 1
FWIW, when I feel the need to capture command keys in a program, I like to do so via the ProcessCmdKey override because I've had issues with those captures in other available handlers. So, you might want to take a look at ProcessCmdKey and see if that will help.
-
please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :
private bool EXIT;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
EXIT = true;
}
}
private void Function()
{
for (int x = 0; x < Max; x++)
{
if(EXIT)
{
break;
}// work on image... OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight))); }
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.DrawImage( image, 0, 0, Width, Height);
}when the Esc key is pressed nothing happens. what i have to do?
sometimes 0 can be 1
What excactly are you trying to achieve? Is it that once the user has pressed the escape key, you want the application to terminate as soon as possible? Or do you have a longish OnPaint method which you want interrupted when the escape key is pressed? The first would be much easier. The second I think cannot work without polling for the EXIT variable within the OnPaint procedure everywhere it could be interrupted, and even then I'm not sure it could work. A call to Application.DoEvents won't help cause in OnPaint you're right within handling the windows message queue and have to finish the loop anyway. The keyboard action only puts a message to the end of your form's message queue, which will not be processed before you leave the OnPaint method. Either you do some crazy unmanaged subclassing, or you split your painting logic into "atomic" operations which you instigate (via this.BeginInvoke) from a different thread. In this thread you can capture the EXIT flag set by the main thread via the escape key and if it's true stop pouring BeginInvoke(PaintNextPart) delegates to the main thread. You need a little more then, of course, and a firm grip of threading and locking mechanisms, or you'll freeze your form.