Search code examples
c#visual-studioconsolebreakpointsvisual-studio-debugging

Difference in state in Console App between Console.Readline() and setting a break point


The following code behaves strange and I try to understand why:

NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080);
EmbeddableDocumentStore db = new EmbeddableDocumentStore();
db.DataDirectory = @"C:\Google Drive\TradingFramework\RavenDatabase\";
db.UseEmbeddedHttpServer = true;
db.Initialize();

bool halt = true;

Console.WriteLine("Read to try server");
Console.ReadLine();

The code above "bool halt" initializes a RavenDB database but I it is irrelevant what it does for the purpose of this question. I ran 2 scenarios:

  • a) put a break point in Debug mode within VS2012 at the line bool halt = true;. At that point I am supposed to be having access to a created web server listening on localhost:8080. That is not the case. Even putting a wait loop into the code before the break points does not make a difference, nor does a Thread.Sleep(xxx).

  • b) I removed the breakpoint and code execution halts and waits for user input at line Console.ReadLine();. In this case the server at localhost:8080 responds and works just fine.

Why is that the case? Why is something not executing when I set a break point vs Console.Readline()? Please note I am strictly just testing/developing but I try to understand what difference it makes? I was always under the impression a) and b) is one and the same, it interrupts code execution.


Solution

  • Console.Readline() doesn't break execution, it just waits for input from the user. Other threads in the application continue to run as normal.

    A break-point actually stops execution to allow you to look at the state of the application. In this state all threads a paused so there's nothing to respond to port 8080.

    You could open a command prompt and type nestat -a, in which case you should see in either scenario that there's a process listening on port 8080.