0

The following code is causing the console to close (exit code 0, telling me that the program is finished). Weird thing is: it doesn't even go to the switch (choice) statement. The debugger shows me that the Console.ReadLine() statement is reached, but not the switch(choice).

Please note:

  • The console closes after I enter a number (1 or 2) and hit Enter.
  • I set breakpoints on the switch statement and it does not reach this line of code. I verified that using the debugger. It's closing the cmd right after I hit enter. This seems to be the last line of code reached.
  • I've used that kind of menu in another class of my program without any issues whatsoever.
  • I'm calling this method (PostTestMenu()) from a System.Threading.Timer

Why would the program stop even though there is still code to be executed and the debugger clearly states that it doesn't even reach the subsequent code?

protected void PostTestMenu ()
    {
        string choice;
        Console.WriteLine(" ");
        Console.WriteLine(" ");
        Console.WriteLine(" ");
        Console.WriteLine("############## What next #############");
        Console.WriteLine("Repeat                               1");
        Console.WriteLine("Next                                 2");
        choice = Console.ReadLine();
        switch (choice)
        {
            case "1":
                Setup();
                break;
            case "2":
                program.Hauptmenue();
                break;
        }
    }
Guntram
  • 414
  • 2
  • 5
  • 18
  • Hit `Enter` key – sujith karivelil Aug 22 '18 at 09:55
  • 1
    Thanks Sujith. I edited my question (The console closes after I enter a number (1 or 2) and hit Enter.). – Guntram Aug 22 '18 at 09:57
  • 2
    After your methods `Setup` or `program.Hauptmenue()` end, application is closed because there's no more code to execute. Put another `ReadLine` after your `case` block, like this: `Console.WriteLine("Press enter to end application"); Console.ReadLine();`. – Nino Aug 22 '18 at 09:58
  • 1
    Should the program terminate after the switch? – Alfie Aug 22 '18 at 10:00
  • 2
    The line probably *does* execute, however the app just finishes after your switch making the console close. To avoid that, call another `Console.ReadLine` at the end. This forces user to press a key before the console closes. – MakePeaceGreatAgain Aug 22 '18 at 10:01
  • 2
    Cannot reproduce, https://dotnetfiddle.net/Ti2gCf – V0ldek Aug 22 '18 at 10:04
  • The methods Setup() and Hauptmenue() both contain menues just like that. They have the same strucuture, meaning that there are ´Console.ReadLine()´ in both methods. It just doesn't go there. – Guntram Aug 22 '18 at 10:42
  • @Guntram can you please expand V0ldek's solution so your issue is reproduced there? – rs232 Aug 22 '18 at 11:23
  • @rs232 done, unfortunately I can't get the code to run. Also I'm kind of suspecting the Timer to cause the issue. It might be related to the threading it is doing. – Guntram Aug 22 '18 at 11:36
  • @Guntram The V0ldek's code perfectly compiles and runs. What exactly do you mean when you write that you can't get it to run? Can you compile it? Can you run it? If no, then what exactly happens when you try to compile/run it? – rs232 Aug 22 '18 at 11:39
  • @rs232 Sorry. Thought it would auto-save. V0ldek's code ran perfectly, I updated it because it was read only https://dotnetfiddle.net/CVRBfF – Guntram Aug 22 '18 at 11:43

1 Answers1

0

Now with the working example at https://dotnetfiddle.net/CVRBfF, your problem is obvious. What happens when you launch your program?

  • your Main method gets launched
  • Main instantiates a new Program object
  • Main sets a timer to tick after 100ms
  • Main completes its execution and exits
  • upon exiting Main, the program execution completes, the system discards all the program's resources including pending timers as the program has quit anyway.

Your application quits long before your 100ms interval passes, so your PostTestMenu method never gets called.

If you want your application to show something, you definitely need to give it some time for that before quitting. The simplest way would be to add

Console.Readline();

to your Main(), right before the closing }.

rs232
  • 1,248
  • 8
  • 16
  • Thanks for your reply, I appreciate your effort and I believe that we're on track for the solution. However the PostTestMenu method does get called even without adding a Console.ReadLine() (which I tried). But I believe that this threading is the underlying issue. I'll investigate some more. – Guntram Aug 22 '18 at 12:05
  • While it does not work at dotnetfiddle.net, it perfectly works when I compile this code into a real console application with Visual Studio. Did you try it this way? – rs232 Aug 22 '18 at 12:22
  • Yes, the dotnetfiddle version is the one I'm working on. By now I'm pretty sure that it's related to the timer threading. I'll investigate more on how to create thread safe timers. – Guntram Aug 22 '18 at 13:57