Avoiding "Press any key to continue" when running console application from Visual Studio - visual-studio

When running a console application in Visual Studio via "Start without Debugging" (Ctrl+F5), the console remains open at the end of the run asking to
Press any key to continue . . .
thus requiring to activate the window and hit a key. Sometimes this is not appropriate.
Why this matters:
At the very moment I write json serialisation code, my workflow goes like this:
adapt c# code
run a console app that writes file out.json
view out.json in the browser with a json viewer
do this again and again, no need to debug anything, just trimming serialisation and check output is good.
It is workflows like this, where the "press any ..." behavior is hindering as it requires the steps
activate the console window
press key
.
No answers:
Starting the application outside VS in a separate console is not an answer.
Saying, you dont need this.

I'm pretty sure that you cannot affect or change this behavior.
As you mention, it has nothing to do with your application itself, because it doesn't do it when you double-click on the EXE. You only see this effect when you run the app from within Visual Studio without the debugger attached.
Presumably, when you invoke Ctrl+F5, Visual Studio is running your app in a particular way that causes the console window to remain open. I can think of two ways it might be doing it:
%COMSPEC% /k "C:\Path\To\YourApplication.exe"
or
%COMSPEC% /c ""C:\Path\To\YourApplication.exe" & pause"
With either of these, the pausing behavior you're seeing is baked right into the command used to launch your app and is therefore external to your application. So unless you have access to the Visual Studio sources, you're not going to change it. Calling an exit function from your app won't have any effect because your app has already quit by the time that message appears.
Of course, I can't see why it really matters, aside from an issue of curiosity. This doesn't happen when you start the app with the debugger attached, which is what you'll be doing 99% of the time when you launch the app from the IDE. And since you don't ship Visual Studio along with your app, your users are going to be starting the app outside of VS.
In response to the updates made to your question, the best solution would be to change your app so that it is not a console application. This behavior doesn't affect standard Windows applications; when they get closed, they close for good.
If you do not require any output on the console window, then this is very simple to do: just change the "Application type" in your project's properties. A Windows Forms application will work just fine. If you do not display a window (aka form), one will not be automatically created. This is the difference between regular Windows applications and console applications, which always create a console window, whether you need one or not.
If you do need to display output on the console window, you have a couple of options:
Create and use a simple form with a ListBox or ListView control. Each line that you would normally output to the console, you add as a new item to the list control. This works well if you're not using any "advanced" features of the console.
P/Invoke and call the AllocConsole function to create a console that your Windows application can use. You do not need a form for this.

I found a solution that works if you are using python (I could not test anything else).
You need to go to
Tools -> Options -> Python Tools -> Debugging
Uncheck Wait for input when process exits normally.
I hope you can apply this somehow to your problem.

2020 UPDATE : Microsoft has listened.
It also closes the console in "Start Without Debugging" mode ;)
The setting is a little buried, but works :

Well, at least in Visual Studio 2010, typing
Console.ReadKey(true);
Removes the "Press any key to continue....."

According to the VS2019 documentation:
Automatically close the console when debugging stops: Tells Visual Studio to close the console at the end of a debugging session.
It works, but only if you make sure your project starts with the debugger on. This sounds trivial, but I was trying at first with a solution with two projects, one Console one to copy files to use in my app, the other to run the actual app. I set the Console one to Start without debugging because I don't need debugging on it, but that did not close it after it ran. Only when setting it to Start (with debugging) this option worked.

In vs2017 you have to uncheck the python environment setting under the vs-options:
in german: Auf Eingabe warten, wenn der Prozess normal beendet wird

Related

Use a console window when debugging unit-tests in Visual Studio

When I debug a unit-test with Visual Studio 2017, all the console output is logged and I can examine it by clicking the "Output"-link in the result box of the unit-test (I'm using the built-in Test-Explorer).
However, since I do log a lot to console at runtime which is even color coded so important stuff is more visible, I can't see this color coded output using the "Output"-link, because it's just plain black text on white background.
Is it possible that Visual Studio shows all the output directly in a console window when debugging the tests so I see the output as I would see it when actually running the application outside of a test?
You cannot (or it is very tricky at least) open a console window from a unit test - I've tried with the top 2 answers from the following post, and they didn't work: Show Console in Windows Application?
You can make the Debug.Write... methods write to the Console when you are running/debugging the application itself (not the unit tests) with this code, though:
ConsoleTraceListener listener = new ConsoleTraceListener();
Debug.Listeners.Add(listener);
Any calls to Debug.Write... methods after this code will also output to the console.
Standard output is the only way to display the test output, when we use some output method in our test like Debug.WriteLine(); Console.WriteLine(); etc.
I also tried to change the font of the test output but it seems there is no that option is VS 2017, even someone though someone mentioned this before:
Is there a way to change the Font used in the VS2012 Test Output Window?
Similar case discussed in this thread, even some reply claimed that we could manually launch the console window, but they are 100% sure about it.
If you really need change the font or the color of the test output, in Visual Studio you go to Help -> Send feedback to VS developing team to provider your suggestion .

How do I write to the CommandPrompt from Windows GUI?

Operating Environment: Windows 7, Visual Studio 2010, CLR GUI.
So I've been given the unglorious task of enhancing a GUI application that is started from a command prompt. Because it is. Because poor design decisions by previous implementers. Anyway, it launches one of several GUIs depending upon the input arguments.
I'd like to be able to print back to the same command prompt window if (when) the user types something that the code doesn't understand.
Here's what I've tried (none of which output anything):
int main( array<System::String^>^ args )
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
OutputDebugString("hello");
Trace::WriteLine("hello");
Debug::Trace::WriteLine("hello");
Console::WriteLine("hello");
std::cout << "hello";
printf("hello");
return 0;
}
Thanks in advance!
Update: I don't want to use AllocConsole(), as that opens a new console that disappears along with all of the data when the application exits. Similarly, a pop-up message box won't work. I'm looking for a way to make the output persistent.
The only way I can get output from the application to date is via a message box (non-persistent) or opening a new console that disappears when the application exits (via AllocConsole() ). And I'm running from a command prompt, not the debugger's "Play" button.
Update
Why the down-vote for not doing research? I spent a day trying to solve this, looking through dozens of posts trying to find a solution, and to date I've found others looking for the same answer, but not finding it. AllocConsole() or changing the project type is always the solution, but neither is a solution for me.
Update
I added the "full code", which is the 2 statements. THAT IS ALL THE CODE. So simple. I'm skipping the start of the GUI because I don't care about that right now, I just want it to print back to the console where the application was started. The most basic HelloWorld. If there are project settings I need to post, I don't know which ones would be relevant. This is where I want to print to the console, before the GUI is ever up. I can't show the GUI if there is an error in the user input.
Right click on the project, select Properties
Under Linker -> System, Change Subsystem from Windows to Console.
A Windows subsystem application cannot write to console, but by changing the subsystem to Console (which can write to the calling console), the Form part of the application can still work (tested in Visual Studio 2010).

Redirecting 'nunit-console' output to the Visual Studio output window

I am testing an F# project using NUnit. On the debug tab of project configuration I have set the debugger to use an external program which is nunit-console here and the working directory to the debug folder of my project. I prefer using nunit-console with the debugger since the GUI version doesn't hit the test file's breakpoints.
When I debug the test the console window appears and disappears and there is no chance to read the output. I have tried Console.Readline(), but it doesn't work because when I directly run the test from a terminal using nunit-console, it fails due to this command.
Is there a way to redirect the nunit-console output to the Visuals Studio's output window?
UPDATE: I could use Thread.Sleep() to delay the nunit-console.exe when I run the test from the console. But from Visual Studio it doesn't happen so I am pretty sure that nunit-console.exe fails to read the test file when the command is issued by Visual Studio. Still, it would be very nice to be able to read the console output, thus the redirection is still desirable.
Either use Tools->Options...->Debugging->General:"Redirect all Output Window text to Immediate Window" and then make sure that the "Immediate Window" is visible with Debug->Windows->Immediate.
Or use NUnit's "/wait" command line option.
Unless I am missing something, you should be able to hit all your breakpoints with the GUI as well, set the startup project to nunit.exe and pass the name of your test DLL as a command line parameter. You will hit the breakpoints in Visual Studio, and your print statments will be in the GUI's output tab.

Using "run as" with Visual Studio debugger

Is there any way to use the "Run As" option in Windows XP in conjunction with Visual Studio's debugger, to debug an issue that occurs in my application only when certain users are logged in?
I have ran the application from my machine using "run as" to pretend to be the user in question, and I got the same error as they did. I would like to debug this error and see where and what exactly is causing it. The error occurs specifically when a certain domain user is logged in, and never otherwise.
Is there any script or approach I can take to debug this error; that is too launch the application, as the problem user, and then use the debugger?
Trying to attach to the process didn't work since it was a C# managed process and VS didn't let me attach.
The first two options that come to mind are...
Log onto the machine as the user (simplest approach)
Right-click on the Visual Studio executable and run as that user.
I think that you could edit the .config file to use impoersonation, but I'm not sure if that will result in the app running truly the way it would for the user.
Although with good error handling, the error message itself should be enough to tell you where in the code the problem is... At the very worst, you could compile it in debug mode (so you have all the symbols) and add some global error hanlding and get the exact stack trace...
I use David Strattons second solution (run as Administrator) because my application requires administrator privileges (-> elevated).
Another solution could be to start the application as the user and use "Debug | Attach to process..."

MS Access 2003 does not enter into debug mode and ignores breakpoints

I developed a small VBA procedure in MS Access 2003 module (just one public Sub)
The database is locked for me only, nobody else has access to the file.
My code works but there is a small bug I want to find and fix
I need to debug my VBA code. I put breakpoint at the first line of the procedure.
However, when I run this code, it never stops at the breakpoint and never enters to debug mode. Seems like VBA debugger is not working or disabled. I was not able to find any option how it is possible to disable/enable VBA debugger, I supposed it should be always enabled. Now I can debug this code only with the help of putting a lot of message boxes, but it takes a lot of time...
Please see:
ACC2002: Breakpoints Are Ignored in Visual Basic for Applications Code
Enable the Use Special Access Keys startup option.
To do so, follow these steps:
Open the database in which the breakpoint has been set.
On the Tools Menu, click Startup.
In the Startup dialog box, click to select the Use Special Access Keys
check box.
Click OK to close the Startup dialog box.
Close and then reopen the database.
Run the code that contains the breakpoint. Note that execution of the
code pauses at the breakpoint, as you
would expect.
Office 2010 Steps to resolve:
File
Options
Current Database
Make sure "Use Access Special" is checked.
Close and reopen Database.

Resources