简体   繁体   中英

C# Console.ReadLine() never reads input on VSC in Linux

I've got a Console.ReadLine() inside a finite for loop that never ends reading.
I am using VS Code on Linux Mint. I execute by pressing F5.

using System;

class Person
{
    public string Name { get; set;}
    public override string ToString()
    {
        return "My name is " + Name;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int n = 3;
        Person[] p = new Person[n];

        for (int i = 0; i < n; i++)
        {
            p[i] = new Person()
            {
                Name = Console.ReadLine()
            };
            Console.WriteLine("I just read " + p[i]);
        }

        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(p[i].ToString());
        }
    }
}

I expected to input three names and then output them.
I input by typing a name and then pressing Enter.
The issue is that I can keep inputting forever and that Console.WriteLine("I just read " + p[i]); never gets executed. This happens in the Debug Console.

I can't reproduce this on Windows or Ubuntu. My guess is that Mint buffers the output.

Try adding Console.Out.Flush(); after Console.WriteLine("I just read " + p[i]);

This is still an issue on Linux. Running Visual Studio Code, in debug mode. Output is being directed to Debug Console, but input is not being considered.

It only works when running from command line, but that basically disallows for debugging.

Though the answer for this problem is here Debug Console window cannot accept Console.ReadLine() input during debugging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM