So, i've created a C# console application, at the end of the main() i've console.readline() function. But in one of the conditions, i want the console application to close by itself. I tried using Environment.Exit() function but the console doesn't close. Please provide a solution for it.
Asked
Active
Viewed 214 times
-1
-
6Just make sure it doesn't hit the Console.Readline() in that case? – Dnomyar96 May 20 '21 at 06:53
-
1You can't. There is two case: 1) User opened a console then executed your script: when your program end the console will remain open. 2) User double clicked your script file: console will close at the end of the script – Ludovic Feltz May 20 '21 at 06:55
-
There's no such thing as `Environment.close()`. There _is_ `Environment.Exit()`, which works fine. As long, of course, as you remember to call it _before_ your program gets stuck waiting on a call to `ReadLine()`. See duplicate. Alternatively, just change your code to not wait for user input in the scenario where you want it to close on its own. – Peter Duniho May 20 '21 at 07:26