Console Window Disappearing
-
19 มกราคม 2552 22:04Hello all!
I'm new to this IDE (Visual studio professional 2008) but I need to use it now cause I'm coding with Spec#. The problem is (as always) that the console window (since I'm doing text-based stuff only) disappears as soon as the program reaches an end. After searching every corner of the web, what solution I find is to add Console.ReadLine(); at the end of the program (or something similar).
This solves the immediate problem, but whenever the program throws an exception (for example with the assert command) I cannot read the output of it, but the program quits at once, and this is quite annoying. There must be a setting somewhere (Because in Visual Studio Express 2008, which I used before, there was always a "pause" command added to the end) to fix this, or isn't there?
/Peter
ตอบทั้งหมด
-
20 มกราคม 2552 14:42ผู้ดูแล
All editions of VS work the same way. If an exception is thrown then it will appear in the output window of VS.
When you run the code under a debugger (F5) then VS will not pause the app at the end because it runs until it reaches a breakpoint or quits. Hence if you want to debug your code then you'll need to set a BP and step through it. If you just want to run your app and see the output then use Ctrl+F5. This runs the app outside the debugger which means exceptions and BPs aren't handled.
If you want to pause your app in all cases then you can use Console.ReadLine. However this will impact your non-debug code as well. Therefore you'd want to wrap it in an if statement:
if (System.Diagnostics.Debugger.IsAttached) { Console.WriteLine("Press any key to continue..."); Console.ReadKey(); };
Not elegant, but it works. The final option that might work (haven't tried it) is to change the debug start command to use cmd.exe /K <prog> instead. This is how the code is run when you use Ctrl+F5 but I'm not sure how it impacts the debugger.
Michael Taylor - 1/20/09
http://p3net.mvps.org- ทำเครื่องหมายเป็นคำตอบโดย Michael Sun [MSFT]Microsoft, Moderator 30 มกราคม 2552 9:39
-
20 มกราคม 2552 17:20Thank you for the tip. But just to see that there are no misunderstandings, my problem is that when I do run the program without debugging (CTRL + F5) it does not pause when the program crashes or ends. And if I get it right, that code would help me pause the program when debugging.
When I tried Visual Studio Express, it did an automatic pause when pressing CTRL + F5, but now it doesn't. But is it supposed to do that?
/Peter
-
20 มกราคม 2552 17:27ผู้ดูแล
Yes and no. Ctrl+F5 should pause the application at the end however it will not help you with debugging because the debugger won't stop the app. The most you'll see is the output that your console app prints. I'm not sure why you wouldn't see the app pause in VS if you are using Ctrl+F5 unless it isn't mapped to the correct command. It should be mapped to Start Without Debugging.
Michael Taylor - 1/20/09
http://p3net.mvps.org- ทำเครื่องหมายเป็นคำตอบโดย Michael Sun [MSFT]Microsoft, Moderator 30 มกราคม 2552 9:39
-
21 มกราคม 2552 10:14Yes, exactly that is my problem, it doesn't pause when running "Start without debuggin". I was going to try to add /K as an argument, but where exactly do I do that. I checked the menus but can't seem to find a proper place to fix it. Can I do that both for debugging and for running without debugging?
/Peter
-
21 มกราคม 2552 14:52ผู้ดูแล
For debugging you would normally set that in the Project\Debug property page. Within there is an option to set the program to run and the arguments to pass to it. Like I said though I'm not sure if it'll work properly with the debugger though because it would be running a separate program.
I took a closer look at the processes being run by VS. When debugging VS starts the app directly so that it can debug it. When you run without debugging though it actually executes the following command:
%comspec%\cmd.exe "/c "<path>\app.exe" & pause"
Because the IDE doesn't need to debug the app it just runs the app via the shell.
BTW make sure that under Project\General that you have the project type as console application. If you don't then you won't see the command window in either case.
If none of this helps you then you might need to repair the VS installation and reapply SP1. I don't believe there are any environment related settings that would impact VS in this regard but it would seem the most likely cause because the debugger itself is pretty static.
Michael Taylor - 1/21/09
http://p3net.mvps.org- ทำเครื่องหมายเป็นคำตอบโดย Michael Sun [MSFT]Microsoft, Moderator 30 มกราคม 2552 9:39
-
5 พฤษภาคม 2555 1:07I just had this happen when working on a native app. The problem was that I had pasted in a string into the project properties Debugging /Command Arguments, and the argument string I pasted in had some non-Windows newlines at the end. I had copied the string from a *nix file so the newline character was the wrong encoding, so it didn't show up when pasted into VS properties. It also doesn't show up when pasted into Notepad. Evidently that messes up the cmd.exe arguments so it doesn't run the "pause" command.