Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Console window cannot accept Console.ReadLine() input during debugging

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.

like image 745
James L. Avatar asked Dec 17 '16 04:12

James L.


1 Answers

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

like image 128
mrsauravsahu Avatar answered Oct 08 '22 02:10

mrsauravsahu