mercredi 25 février 2015

Press key to break a loop

C# windows form has problem that when a loop is executed, it disables all other events. So when a loop is too long, I can't stop the program properly (can't press the close button). Anyone know how to solve this problem, please help. To simplify the problem, here is a sample code.



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.S)
{
CheckLoop();
}
}

void CheckLoop()
{
progressBar1.Maximum = 9999999;
progressBar1.Value = 0;
while (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++;
if (Control.ModifierKeys == Keys.Alt)//Did not break when press Alt???
break;
}
}
}


The question is how to make the Alt key works the way it supposes to work? Thanks for reading.


Aucun commentaire:

Enregistrer un commentaire