64

VSCode Version: 1.8.0

OS Version: Win10 x64

Steps to Reproduce:

  1. Create a new .net core cli app using "dotnet new"
  2. Open the folder using VS code
  3. Add two lines of code in Program.cs

    string a = Console.ReadLine(); Console.WriteLine(a);

  4. Switch to VS code debug window and start debugging, Debug Console window shows, and displays the first "Hello, World." output, and stops on the line of Console.ReadLine(), enter anything in the Debug Console and press Enter will be given err message of "Unable to perform this action because the process is running."

The question is how and where to enter text for Console.ReadLine() to accept during debugging, if I open a new cmd.exe and do a "dotnet run" it works fine, but in Visual Studio Code Debug Console it's not working.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
James L.
  • 876
  • 1
  • 9
  • 15
  • 1
    https://github.com/OmniSharp/omnisharp-vscode/issues/1027 – Hans Passant Dec 17 '16 at 04:46
  • Thanks Hans, the link you posted describes exactly the same problem I'm seeing, and it's resolution is to set externalConsole to true to bring up a new console to debug the app which can accept keyboard input, that actually solves the issue. However I still wonder if there is a way to do this within VSCode's UI (either from debug console or integrated terminal or something else). – James L. Dec 17 '16 at 12:44
  • This still seems to be a problem with v1.40.1 (Nov-2019). The accepted answer still resolves it. – Monty0018 Nov 28 '19 at 06:19

2 Answers2

136

To read input whilst debugging, you can use the console property in your configurations in launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "program": "${workspaceFolder}/bin/Debug/net5.0/your-project-name.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "console": "integratedTerminal"
        }
    ]
}

You can either use "externalTerminal" or "integratedTerminal". The "internalConsole" doesn't appear to be working.

I use the integratedTerminal setting, as the terminal is inside VSCode itself. You will now be able to read input with Console.ReadLine();

Note: Also, internalConsole doesn't work, and it is by design. The reason this is, is because internalConsole uses the Debug Console tab to show the output of the Console.WriteLine. Since the input box in the Debug Console is used to run expression on the current stack, there's no place to pass in input that will go to Console.ReadLine. That's the reason you'll have to use something like integratedTerminal.

The screenshot below shows that the VSCode team knows this -

console option with value internal console will not be able to read ser input

mrsauravsahu
  • 2,046
  • 2
  • 15
  • 21
  • 2
    Excellent and the right answer! This solved my problems (running apps and reading key feedbacks in VSC without leaving it). Since when launch.json supports the new console property? The time I submitted my question VSC only supported the externalConsole property. – James L. Sep 30 '17 at 16:09
  • 1
    I'm glad that the solution worked for you. I'm not entirely sure when they added it though. – mrsauravsahu Sep 30 '17 at 16:15
  • 1
    Thanks a load; switching from "internalConsole" to "integralTerminal" fixed the problem and I'm back to finding bugs. – Poetically Psychotic Jan 05 '21 at 05:10
  • How it is possible that 4 years later the default internalConsole still doesn't work? – iamnicoj Mar 04 '21 at 02:34
  • 5
    Unfortunately, that's not enough. To make it work: 1. Alter `"console": "integratedTerminal"`. 2. Right next to it append `"internalConsoleOptions": "neverOpen"`. 3. Now, the irritating ***Debug console*** isn't appearing when you run your app with *Ctrl+F5* and you can see the ***Terminal*** tab where the output appears. – Григорий Mar 08 '21 at 10:23
  • I've updated the answer for the changes required for .NET 5. Also, added the explanation why `internalConsole` doesn't work. – mrsauravsahu Mar 08 '21 at 19:18
  • 1
    "_there's no place to pass in input that will go to Console.ReadLine_" Too generous. MS has better alternatives than "sit and spin". For example, if you're in a `Read` state, you can have the debugger switch over. Maybe if you have a syntax error on the expression entered, you pass it as text to the running app. I'm not saying it's trivial to improve, but devs handle much more nuanced states regularly. Use `readAwareConsole` if you have to be defensive [MS], but have some sort of alternative to what we have now. **What we have now is not an inherent limitation of software design.** – ruffin May 04 '21 at 18:55
  • Doesn't work in VSCodium 1.67.2. Could **free-omnisharp-vscode** be the reason? – Old Skull Jun 11 '22 at 10:38
-7

i am pretty new to c#-visual studio debugger...

try setting a breakpoint before your

Console.Readline()

and debug it by stepping through your code F10 (not F11).

it should stop at

Console.Readline()

and wait for your input.

canoodle
  • 506
  • 5
  • 10
  • Thanks for the answer but I'm using the new Visual Studio Code instead of Visual Studio. Your solution isn't working. – James L. Dec 17 '16 at 14:11