Is there anyway I can break from an infinite loop with a "hotkey" so to say?

Viewed 3594

I am writing this program that I want to run forever via a while loop and whenever the user presses a certain key on their keyboard it exits the program. I've looked everywhere but I have only seen KeyEvents, but the WindowsForm isn't active while the program is running. Anyone have a solution for me?

Edit: The program takes over the cursor so activating an event on the UI is basically impossible

Edit Two:

    public void MainMethod() 
    {
        while (true) 
        {
            if (checkBox1.Checked == true) state = State.PERFORM_ACTION_ONE;
            if (checkBox2.Checked == true) state = State.PERFORM_ACTION_TWO;
            // More stuff checking which state to assign

            switch (state) 
            {

            case State.PERFORM_ACTION_ONE:
                 DoSomething();
                 break;
            // More cases
            // I want it to be able to break anywhere in the while loop
            }
        }
    }
4 Answers
Related