2

No matter what I do my first Console.Readline entry breaks me out of console. This happens whether I am in debug or release.

I get past my WriteLine

("Which Room Would you like to Trigger? \nRoom 1001 or Room 1002") 

and as soon as I hit the ReadLine the console closes.

I'm still learning C# but I never had this issue with Python raw_input(prompt) which I am assuming is the equivalent to C# ReadLine.

My code is below.

bool loopChk = true;

      do
      {
        Console.WriteLine("Which Room Would you like to Trigger? \nRoom 1001 or Room 1002");
        string line = Console.ReadLine();
        if (line == "1001")
        {
          await objClient.WritePropertyAsync(fqrs[0], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
          Console.WriteLine("You have Triggered Room 1001");
        }
        else if (line == "1002")
        {
          await objClient.WritePropertyAsync(fqrs[1], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
          Console.WriteLine("You have Triggered Room 1002");
        }
        else if (line == "exit")
        {
          break;
        }
      } while (loopChk);
Phi
  • 335
  • 9
  • 22
  • Put a `Console.ReadLine()` after the `do-while` loop. – Sumner Evans Apr 21 '15 at 03:01
  • @jsve Didn't do anything as soon as I enter a value and press enter it closes down. – Phi Apr 21 '15 at 03:09
  • That's what it's supposed to do. Isn't that what you want? Wait until the user presses enter before exiting the program? I guess I don't quite understand your question. – Sumner Evans Apr 21 '15 at 03:11
  • @jvse no it should stay in the do while loop until i type in exit. – Phi Apr 21 '15 at 03:16
  • hmm... have you debugged to see what value `line` has and where the code goes from there? You seem to be on the right track. – Sumner Evans Apr 21 '15 at 03:20
  • Using `await` in console app is a bit tricky - check out http://stackoverflow.com/questions/17630506/async-at-console-app-in-c that show one option. – Alexei Levenkov Apr 21 '15 at 03:24
  • Are you sure that Console.ReadLine() is breaking you out of console instead of something else? e.g. objClient.WritePropertyAsync(...) ? – failedprogramming Apr 21 '15 at 03:25
  • I think something else is going on. I am not getting any value in line. I put a breakpoint in and line = null as I step into the code and then it closes the console, I am not even getting to the WritePropertyAsync method. With a Do-While loop I should be at least running the loop once, but right when I step into the line = Console.ReadLine(); the console closes and my watch on line doesn't show anything. – Phi Apr 21 '15 at 03:29
  • You could check if Console.ReadLine() throws an IOException (test with try/catch block). MSDN documentation tells that ReadLine may throw, although I have to admit that I never saw this happening. – Silverstein Apr 21 '15 at 05:32

1 Answers1

0

Try this instead:

string line = null;

do {
    Console.WriteLine("Which Room Would you like to Trigger? \nRoom 1001 or Room 1002");
    line = Console.ReadLine();

    if (line == "1001")
    {
      await objClient.WritePropertyAsync(fqrs[0], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
      Console.WriteLine("You have Triggered Room 1001");
    }
    else if (line == "1002")
    {
      await objClient.WritePropertyAsync(fqrs[1], "Present Value", a_bOn ? "on" : "off", CancellationToken.None);
      Console.WriteLine("You have Triggered Room 1002");
    }
} while (line != "exit")
Luke Franklin
  • 514
  • 4
  • 13