5

My code consists of something like this:

{
    var comeback = Cursor.Position;
    code goes here
    code goes here
    code goes here
    code goes here
    code goes here
    code goes here
    Cursor.Position = restart;
}

Now, I wish to have this continuously looped until such time as I invoke a keypress to stop.

What I cannot do i write the code for this loop, or is there a different way I should be going about this.

Thanks in advance

5
  • 1
    I don't understand. Are you asking how to write an infinite loop in C#? Try for(;;) Commented Apr 25, 2011 at 13:58
  • Be specific: console, winform, webform or wpf? Commented Apr 25, 2011 at 13:58
  • Yes I wish this to be a infinite loop, which will have a keypress event to stop Commented Apr 25, 2011 at 13:59
  • 4
    Now that we know this is a WinForms application, you do not want an infinite loop on your UI thread. That will lock up your application, and prevent it from either responding to user input or updating anything displayed on the screen. Commented Apr 25, 2011 at 14:05
  • Infinite loops in a WinForm application is a horrible idea :$ Commented Apr 25, 2011 at 14:09

3 Answers 3

10
while(!Console.KeyAvailable)
{
    //do work
}
Sign up to request clarification or add additional context in comments.

2 Comments

Late info ("And it is a winform") invalidates this answer.
still, since this is a nice way to do it for consoleapplications you get a +1 from me (other people looking for "keypress loop" might be interested in your answer)
2

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;
    }
    ...
}

1 Comment

@Javed. yeah.. I noticed the added comment.. I am thinking about how to edit my answer..
-2

HAVE A BOOLEAN VARIABLE.

like bool flag = true; while(flag) { your code; }

when the key is pressed, change the flag to false.

1 Comment

How do you propose to check the key press??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.