Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console.In.Peek() always returning EOF after the first read line on Windows #40735

Open
eiriktsarpalis opened this issue Aug 12, 2020 · 4 comments
Labels
area-System.Console help wanted [up-for-grabs] Good issue for external contributors os-windows
Milestone

Comments

@eiriktsarpalis
Copy link
Member

I came across the following behaviour when debugging a System.Console test. Running

static void Main()
{
    while (true)
    {
        Console.Write("Enter some text: ");
        int c = Console.In.Peek();
        string? line = Console.In.ReadLine();
        Console.WriteLine($"Peek: {c} ReadLine: {line}");
    }
}

Produces the following behaviour

Enter some text: test
Peek: 116 ReadLine: test
Enter some text: test
Peek: -1 ReadLine: test
Enter some text: test
Peek: -1 ReadLine: test

Essentially it is impossible to use Console.In.Peek() after the first line has been consumed. Note that this behaviour manifests itself regardless of whether the buffer contains any characters. The following variant will produce the exact same observable behaviour:

static void Main()
{
    while(true)
    {
        Console.Write("Enter some text: ");
        int c = MyPeek();
        string? line = Console.In.ReadLine();
        Console.WriteLine($"Peek: {c} ReadLine: {line}");
    }

    static int MyPeek()
    {
        while (!Console.KeyAvailable)
        {
            Thread.Sleep(10);
        }
        return Console.In.Peek();
    }
}

Note that unix does not have the same behaviour.

@ghost
Copy link

ghost commented Aug 12, 2020

Tagging subscribers to this area: @eiriktsarpalis
See info in area-owners.md if you want to be subscribed.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Aug 12, 2020
@eiriktsarpalis eiriktsarpalis changed the title Console.In.Peek() always returning EOF after the first read line Console.In.Peek() always returning EOF after the first read line on Windows Aug 12, 2020
@eiriktsarpalis
Copy link
Member Author

Related to #39192.

@adamsitnik
Copy link
Member

I assume that this is not a regression and moving it to future

@adamsitnik adamsitnik added this to the Future milestone Aug 14, 2020
@eiriktsarpalis
Copy link
Member Author

I assume that this is not a regression and moving it to future

That's correct, this seems to have been the behaviour for quite a while. The main goal here is achieving consistency with unix behaviour.

@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Aug 31, 2020
@adamsitnik adamsitnik added the help wanted [up-for-grabs] Good issue for external contributors label Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Console help wanted [up-for-grabs] Good issue for external contributors os-windows
Projects
None yet
Development

No branches or pull requests

4 participants