How to keep a .NET console app running? How to keep a .NET console app running? multithreading multithreading

How to keep a .NET console app running?


you always want to prevent using while loops, especially when you are forcing the code to recheck variables. It wastes CPU resources and slows down your program.

I would definitely say the first one.


Alternatively, a more simple solution is just:

Console.ReadLine();


You can do that (and remove the CancelKeyPress event handler) :

while(!_quitFlag){    var keyInfo = Console.ReadKey();    _quitFlag = keyInfo.Key == ConsoleKey.C             && keyInfo.Modifiers == ConsoleModifiers.Control;}

Not sure if that's better, but I don't like the idea of calling Thread.Sleep in a loop.. I think it's cleaner to block on user input.