0

I want the command prompt to be closed immediately whenever it arrives to the Else function. Here :

enter image description here

Everything works properly but the command prompt doesn't close after reading Else function and jumps to the next step, which is :"Console.WriteLine("Okay, Hello");

I'd be glad if you help me! Thanks

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 2
    Isn't the exit in the wrong branch? As is, you tell the user they are *not* allowed if they are <18 but you don't Exit and else you tell the user they *are* allowed but you exit ... – Fildor Aug 23 '17 at 14:40
  • Are you sure you're using an age that fails your condition? The code performs as expected for me if I use an age like 30. However, I just throw it into a new console app's Main method and that's it. Maybe your situation is different. If that is the case, you should elaborate if possible. – Broots Waymb Aug 23 '17 at 14:42
  • @fildor I don't know where it should be. I just want the program to stop working at Else command, but if the IF function is true, to continue moving towards the next step. If you have any tips to fix it, it might help me out, thanks for responding – Luka Partsvania Aug 23 '17 at 14:44
  • What was the input data you used for testing? From the code, it should hit exit if UserAge is >= 18. – Fildor Aug 23 '17 at 14:45
  • @Fildor I used both, over 18 and less than it, but the output is the same, it still outputs the last step, "Okay, Hello". I just want the program to exit whenever else function detects the user is less than 18 y-old – Luka Partsvania Aug 23 '17 at 14:47
  • @LukaPartsvania How are you launching this app? From Visual studio or using .exe? – Siva Gopal Aug 23 '17 at 14:48
  • 3
    As is , the else-path should be executed if UserAge is >= 18. So your `else` does the opposite of what you want right now. If you tried with both < 18 and > 18 and had the same output, then there is something _very_ strange. I recommend you step it through in the debugger and watch the UserAge var. – Fildor Aug 23 '17 at 14:51
  • @SivaGopal Visual Studio – Luka Partsvania Aug 23 '17 at 14:52
  • I just copied your code, moved the `Environment.Exit(0)` into the first if branch and added a `Thread.Sleep(2000)` so I could read the output before closing - works just fine. Maybe you need to post a more comprehensive part of your code to show the actual problem? – LocEngineer Aug 23 '17 at 14:53
  • Strangely, the message texts correspond correctly to the if - condition ( <18 => NoNo, >=18 => ok). – Fildor Aug 23 '17 at 14:56
  • Check the edited question, please. I added a picture instead of a code. – Luka Partsvania Aug 23 '17 at 15:12
  • a) Please leave code instead of screenshots, b) that code works exactly as intended. I don't understand your problem. – LocEngineer Aug 23 '17 at 15:18
  • @LocEngineer It works but I don't want to meet "Okay Hello" if the user's input is < 18, and sorry for screenshot, I couldn't paste the full code – Luka Partsvania Aug 23 '17 at 15:23
  • Could you please just use the debugger? Set a breakpoint on line 16 and make sure "UserAge" has the value you expect, then step on (F10) to see where it is going. – Fildor Aug 23 '17 at 15:29
  • " I don't want to meet "Okay Hello" if the user's input is < 18" - That's impossible with that code. You are *not* entering the else-Path if UserAge is < 18. Look at the output you get. It will be correct as to the if condition. But if you want to exit if user is <18 then you have to place the exit in that path not in the other path. – Fildor Aug 23 '17 at 15:31
  • : | ..... Sorry, guys... I just moved Environment.Exit in the IF branch. and it works properly.. thank you everyone for your attention!!! I just made a mistake so I was talking absurd... TY! – Luka Partsvania Aug 23 '17 at 15:38

1 Answers1

0

To close the command prompt that was used to launch your program, you will need to send the "close" signal to the current program's parent process.

To identify the parent process, see this question.

To send the close signal, use Process.Close.

John Wu
  • 50,556
  • 8
  • 44
  • 80