0

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 :)

maf-soft
  • 2,335
  • 3
  • 26
  • 49
  • `while (true) ;` – Sandris B Apr 20 '20 at 07:24
  • 2
    Run program with CTRL+F5 – Tomas Paul Apr 20 '20 at 07:36
  • Could [Console.ReadKey()](https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey?view=netframework-4.8) work for you here? You specify the key that would need to be pressed for it to close? – sr28 Apr 20 '20 at 07:41
  • @SandrisB, and then I accidentally forget to close it and will be responsible for climate change? I don't see any advantage in your solution other than needing one less using. – maf-soft Apr 20 '20 at 07:42
  • @TomasPaul, even if I wanted to run it without debugging, how can I redirect input then? It doesn't seem to work. – maf-soft Apr 20 '20 at 07:43
  • @sr28, I tried that, but get an error regarding redirected input. ReadKey() doesn't work here. – maf-soft Apr 20 '20 at 07:45
  • @maf-soft, to be honest, I do not know how the `< "filename.in"` and redirection works. Do you have any link to documentation? – Tomas Paul Apr 20 '20 at 10:03
  • @TomasPaul, the Command line arguments box supports the same redirection operators like cmd. So you can enter `param1 param2 < file.in > file.out` and only the 2 paramers arrive in the `args` array. I couldn't find documentation ( seems broken there). Seems it is only documented for C++, but it works for C#, too: https://learn.microsoft.com/en-us/visualstudio/debugger/project-settings-for-a-cpp-debug-configuration?view=vs-2019 – maf-soft Apr 20 '20 at 11:03
  • Maybe you could alternativelly implement `Console.SetOut()` https://learn.microsoft.com/en-us/dotnet/api/system.console.setout?view=netframework-4.8. – Tomas Paul Apr 20 '20 at 11:28

1 Answers1

1

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. enter image description here

  • 1
    This is already mentioned in my question - does it really work for you? – maf-soft Apr 20 '20 at 08:07
  • Yes, It's worked, this is proof: https://i.imgur.com/PiiLJIV.png. I wonder maybe it only work on .NET Core or something – Hoàng Minh Thông Apr 20 '20 at 08:11
  • If it still not work for you. Maybe you will want this statement in end of your Main() function: `Task.Delay(-1).Wait();` – Hoàng Minh Thông Apr 20 '20 at 08:17
  • Hmm, ok, I added the .net-4.6 tag to my question now. For me this doesn't work :( - I only get the pause when run without input redirection. – maf-soft Apr 20 '20 at 08:27
  • What's the advantage of `Task.Delay()` compared to `Thread.Sleep()`? – maf-soft Apr 20 '20 at 08:32
  • 1
    You can see the explain about `Task.Delay()` and `Thread.Sleep()` below, There're no advantage, It's depend on your purpose: https://stackoverflow.com/questions/20082221/when-to-use-task-delay-when-to-use-thread-sleep – Hoàng Minh Thông Apr 20 '20 at 08:39