There are several options depending on your intended use, including calling the same method as described above). From the console application:
bool exitLoop = false;
for(int i=0;i<bigNumber && !exitLoop;i++)
{
if(Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if(ConsoleKey.Escape == key.Key)
{
exitLoop=false;
}
}
}
Windows, , ( ):
public partial class Form1 : Form
{
private bool exitLoop;
public Form1()
{
InitializeComponent();
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
}
public void doSomething()
{
this.exitLoop = false;
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object notUsed)
{
while (!exitLoop)
{
}
}));
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (Keys.Escape == e.KeyCode)
{
e.Handled = true;
this.exitLoop = true;
}
}
}
, - - . , , ThreadPool . , , , , . - , , ...
public override bool PreProcessMessage(ref Message msg)
{
base.PreProcessMessage(msg);
}