Reputation: 2552
I'm debugging my console application with redirected input from a file (Debug / Start Options / Command line arguments < "filename.in"
). That means I cannot use the usual ReadLine()
/ Read()
/ ReadKey()
to keep the console window open after executing, because those would try to read the input from the file.
My current solution is a Sleep()
, can you provide better alternatives?
using System;
using System.Diagnostics;
using System.Threading;
using static System.Console;
class Test
{
static void Main(string[] args)
{
WriteLine("some output");
if (Debugger.IsAttached) Thread.Sleep(Timeout.Infinite);
}
}
Instead of checking for the debugger or #if DEBUG
, I could also check for some addtional command line argument which I only pass from within Visual Studio. This has the advantage that it would work when run without debugging (but then I don't know how to redirect input).
Another alternative is a breakpoint at the end, but then my window is put to the background and I need a click to view it.
The last alternative is redirecting the output as well, and viewing it in a file tab. But somehow there is no way to refresh it easily.
Since VS2019, there is a new option Debugging / General / Automatically close the console, but unchecking it doesn't seem to work when input is redirected, too. I say this is a bug.
So I'm looking for better ways. Extra points for not needing any using
like above :)
Upvotes: 0
Views: 2001
Reputation: 131
If you use Visual Studio 2019 (I don't know other version will work), You can do by
Tools->Options->Debugging-> Uncheck Automatically close the console when debugging stops.
Upvotes: 1