Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Depending on your desired use there are a couple of options, including invoking the same method as described above). From a console app:</p> <pre><code>bool exitLoop = false; for(int i=0;i&lt;bigNumber &amp;&amp; !exitLoop;i++) { // Do Stuff. if(Console.KeyAvailable) { // Read the key and display it (false to hide it) ConsoleKeyInfo key = Console.ReadKey(true); if(ConsoleKey.Escape == key.Key) { exitLoop=false; } } } </code></pre> <p>If you are working on a windows form, every form has a number of key related events you can listen to and handle as necessary (Simplified most of the logic):</p> <pre><code>public partial class Form1 : Form { private bool exitLoop; public Form1() { InitializeComponent(); this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp); } public void doSomething() { // reset our exit flag: this.exitLoop = false; System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(object notUsed) { while (!exitLoop) { // Do something } })); } private void Form1_KeyUp(object sender, KeyEventArgs e) { if (Keys.Escape == e.KeyCode) { e.Handled = true; this.exitLoop = true; } } } </code></pre> <p>Note that this is <em>very</em> simplified - it doesn't handle any of the usual threading issues or anything like that. As was pointed out in the comments, the original go-round didn't address that problem, I added a quick little ThreadPool call to thread the background work. Also note, that the problem with listening for the key events is that other controls may actually handle them, so you need to make sure that you register for the event on the correct control(s). If a windows form application is the direction you are heading, you can also attempt to inject yourself into the message loop itself... </p> <pre><code>public override bool PreProcessMessage(ref Message msg) { // Handle the message or pass it to the default handler... base.PreProcessMessage(msg); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload