since OP thanked me for the first answer, I'll keep that as reference below..
consider having a background thread that does the loop. Then add a key listener in your project (if you have visual studio, open up the properties tab and check out events) by double clicking the KeyPressed event. You'll get something like this:
private bool keyPressed;
public MyClass() {
keyPressed = false;
Thread thread = new Thread(myLoop);
thread.Start();
}
private void myLoop() {
while (!keyPressed) {
// do work
}
}
private void MyClass_KeyPress(object sender, KeyPressEventArgs e) {
keyPressed = true;
}
}
Consider having a thread that listen for a keypress and then set a flag in your program that you check in your loop.
for instance Untested
bool keyPressed = false;
...
void KeyPressed(){
Console.ReadKey();
keyPressed = true;
}
...
Thread t = new Thread(KeyPressed);
t.Start();
...
while (!keyPressed){
// your loop goes here
// or you can check the value of keyPressed while you're in your loop
if (keyPressed){
break;
}
...
}