Hello !

There is a console application ( very primitive ). I have Console.ReadKey() with which I can read the key pressed so that the application closes.

 do { } while (Console.ReadKey(true).Key != ConsoleKey.A); 

For example, it closes when you press A

Question: how can I make it so that the application does not close when ANY key is pressed, but only when I click on the cross

In principle, I tried to do while(true){} but this is very stupid :)

Thank !

    1 answer 1

    Well, combine your two ways:

     while (true) { Console.ReadKey(); } 

    Console.ReadKey(); will not load the processor, and while (true) will not allow the application to close.

    A better solution is to stop the current stream "forever":

     Thread.Sleep(Timeout.Infinite); 
    • I am very confused that while (true) will constantly work in the background. This is normal ? - kxko
    • one
      Console.ReadKey(); will not allow him to constantly work, but only when you press the keys, now I will add the answer in a more correct way - Andrey NOP
    • I merged my two options and did as you have in the first version. Thank you very much and have a nice day :) - kxko