71

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.

3
  • 1
    github.com/OmniSharp/omnisharp-vscode/issues/1027 Commented Dec 17, 2016 at 4: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). Commented Dec 17, 2016 at 12:44
  • This still seems to be a problem with v1.40.1 (Nov-2019). The accepted answer still resolves it. Commented Nov 28, 2019 at 6:19

2 Answers 2

149

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

Sign up to request clarification or add additional context in comments.

9 Comments

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.
I'm glad that the solution worked for you. I'm not entirely sure when they added it though.
Thanks a load; switching from "internalConsole" to "integralTerminal" fixed the problem and I'm back to finding bugs.
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.
"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.
|
-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.

1 Comment

Thanks for the answer but I'm using the new Visual Studio Code instead of Visual Studio. Your solution isn't working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.