Hello, I'd like to know how I can cleanly terminate my C++ console application under windows.

I have a while(true) loop in my code, that keeps polling and Sleep()'ing forever. When I click the close button of the console window, it takes a while before the app closes. How can I detect in the console program that the close button was clicked?

If that's difficult, how can I detect a keypress (like esc) without using a blocking form of input. The polling must be able to continue.