-1

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.

  • 6
    Just make sure it doesn't hit the Console.Readline() in that case? – Dnomyar96 May 20 '21 at 06:53
  • 1
    You 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

1 Answers1

0

You can provide an additional bool parameter for the user to enter when he runs your script, such as consoleClose. In your code check for the consoleClose and if it's true, skip ConsoleReadline() as Dnomyar96 suggested.

Sparkle
  • 681
  • 5
  • 10