Freigeben über


Did you know... If you do a Ctrl+F5 on a console application, the console stays open - #317

If you start the debugger (F5) for a console application, and you don’t have a Stop / Debugger.Break() or some sort of Console.Read() method, the console application window will appear and disappear in a flash.

However, if you start the console application without debugging, the "Press any key to continue…" message appears at the bottom.

image

I would think this behavior should happen when debugging, as you really need to see what is going on. I’m sure there’s a reason, but not quite sure who to ask.

Technorati Tags: VS2005Tip,VS2008Tip

Comments

  • Anonymous
    September 18, 2008
    PingBack from http://www.easycoded.com/did-you-know-if-you-do-a-ctrlf5-on-a-console-application-the-console-stays-open-317/

  • Anonymous
    September 18, 2008
    It's annoying indeed that, when debugging a console application, the process terminates without a message (as when running without the debugger). The solution is to set a breakpoint on the last instruction in the Main method. But not once I forgot to set it and had to re-start the debugger. In my opinion it will be worth to consider it as a fix in a future update for Visual Studio. Thanks.

  • Anonymous
    September 19, 2008
    I agree.  I can find no logical reason for it to not stop while debugging and dose stop when not.  However when has Microsoft done any thing Logical.

  • Anonymous
    September 19, 2008
    Radu, I don't think is annoying, I think is by design, otherwise how would VS know where and when to stop? I somwhat agree with Sara, that if you run in the debugger there should still be the message Press any key to continue... but when you get this msg it means the application has already exited, so what good is it to have this message in the debugger if you can't pause or examine your code (remember, the app is already finished running when you get this message). The only thing you could do is...well...hit any key to exit and go back to VS. The solution is, if you're going to use the debugger, you definitely need to have breakpoints or Debug.Stop() in your code. Leave that job to the developer, not to VS to guess where to stop/pause. Thanks!

  • Anonymous
    September 19, 2008
    I usually put the body of my main into a try/finally block. The finally block then does a check to see if it is running in the debugger and breaks if that's the case. Check out http://blueonionsoftware.com/blog.aspx?p=1e4f3e2c-c631-40a7-9176-25a677c93b0c on how to do this.

  • Anonymous
    September 19, 2008
    WOOT!!... I've always added Console.Read() to my little console apps, to do just that nice... yeah, it seems backwards, but I don't care

  • Anonymous
    September 19, 2008
    The comment has been removed

  • Anonymous
    September 21, 2008
    In addition to the above suggestions, you can use the Debugger.IsAttached property to conditionally wait for user input, and there are other paths e.g. directives. <pre> if (System.Diagnostics.Debugger.IsAttached) {    Console.WriteLine("Press any key to continue . . .");    Console.ReadKey(); } </pre> Stop complaining about Microsoft products, they are better than the rest. The limitations are in your mind, the possibilities are endless.

  • Anonymous
    September 21, 2008
    "Stop complaining about Microsoft products, they are better than the rest. The limitations are in your mind, the possibilities are endless." Very funny.