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

System.InvalidOperationException: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read. #80695

Open
BDisp opened this issue Jan 16, 2023 · 5 comments
Labels
area-System.Console help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@BDisp
Copy link

BDisp commented Jan 16, 2023

Description

I'm having the follow error when I resize the console height to zero or almost zero and pointing the mouse to the available console space:

System.InvalidOperationException: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

This is happening on Windows and WSL using cmd, pwsh and conhost. As I'm reading escape sequences from Console.ReadKey (true) in a console with a height equal to zero causes the System.Console.dll crash. The problem is I can't force resizing the console. Do I still need to using for Windows the Win32 API Console by enabling ENABLE_VIRTUAL_TERMINAL_PROCESSING to be able to use escape sequences or does System.Console allows that feature? Another question is many escape sequences request are not available in Windows and WSL, is there any configuration I can use or is a limitation?

Reproduction Steps

To test this issue please clone the https://github.com/gui-cs/Terminal.Gui repo and run the UICatalog project with the argument -usc, which will use the NetDriver using the System.Console. Thanks.

Expected behavior

I expected the console doesn't crash if height is equal or almost equal to zero. With almost zero I mean when the height is less than one and greater than zero. If it can't read with this size then it must check first for its size and not try to read any keystroke or mouse movements.

Actual behavior

The console really crash and disappear from the desktop, causing the errors already mentioned and others like pipe error.

Regression?

No response

Known Workarounds

No response

Configuration

Net6.0, Net7.0
OS: Windows 11 Pro Version: 22H2 Compilation: 22621.1105
Architecture: x64

Other information

No response

@jeffschwMSFT jeffschwMSFT added untriaged New issue has not been triaged by the area owner area-System.Runtime labels Jan 16, 2023
@ghost
Copy link

ghost commented Jan 16, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I'm having the follow error when I resize the console height to zero or almost zero and pointing the mouse to the available console space:

System.InvalidOperationException: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

This is happening on Windows and WSL using cmd, pwsh and conhost. As I'm reading escape sequences from Console.ReadKey (true) in a console with a height equal to zero causes the System.Console.dll crash. The problem is I can't force resizing the console. Do I still need to using for Windows the Win32 API Console by enabling ENABLE_VIRTUAL_TERMINAL_PROCESSING to be able to use escape sequences or does System.Console allows that feature? Another question is many escape sequences request are not available in Windows and WSL, is there any configuration I can use or is a limitation?

Reproduction Steps

To test this issue please clone the https://github.com/gui-cs/Terminal.Gui repo and run the UICatalog project with the argument -usc, which will use the NetDriver using the System.Console. Thanks.

Expected behavior

I expected the console doesn't crash if height is equal or almost equal to zero. With almost zero I mean when the height is less than one and greater than zero. If it can't read with this size then it must check first for its size and not try to read any keystroke or mouse movements.

Actual behavior

The console really crash and disappear from the desktop, causing the errors already mentioned and others like pipe error.

Regression?

No response

Known Workarounds

No response

Configuration

Net6.0, Net7.0
OS: Windows 11 Pro Version: 22H2 Compilation: 22621.1105
Architecture: x64

Other information

No response

Author: BDisp
Assignees: -
Labels:

area-System.Runtime, untriaged

Milestone: -

@BDisp
Copy link
Author

BDisp commented Jan 18, 2023

I think I discover the cause of this error when resizing the height to 0. I also submitted a PR gui-cs/Terminal.Gui#2294, after I found what was causing an error on WindowsDriver when resizing to zero when the console has no buffer, that is, if the buffer size is equal to window size. The workaround was forcing the minimum window height to 1. The WindowsDriver is using two output handles, OutputHandle representing the original output and the ScreenBuffer which will call CreateConsoleScreenBuffer and SetConsoleActiveScreenBuffer. Only the OutputHandle can be set a height to 0 and the ScreenBuffer must have a minimum height of 1, even in the case where we don't want use buffer. The API has a flag called HeightAsBuffer which if it's true the maximum buffer height will be used, if it's false the buffer size will be equal to window size, but we have to be careful to setting the buffer height to 1 if window height is 0.
Do the System.Console must having similar approach?

@ghost
Copy link

ghost commented Jan 18, 2023

Tagging subscribers to this area: @dotnet/area-system-console
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I'm having the follow error when I resize the console height to zero or almost zero and pointing the mouse to the available console space:

System.InvalidOperationException: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

This is happening on Windows and WSL using cmd, pwsh and conhost. As I'm reading escape sequences from Console.ReadKey (true) in a console with a height equal to zero causes the System.Console.dll crash. The problem is I can't force resizing the console. Do I still need to using for Windows the Win32 API Console by enabling ENABLE_VIRTUAL_TERMINAL_PROCESSING to be able to use escape sequences or does System.Console allows that feature? Another question is many escape sequences request are not available in Windows and WSL, is there any configuration I can use or is a limitation?

Reproduction Steps

To test this issue please clone the https://github.com/gui-cs/Terminal.Gui repo and run the UICatalog project with the argument -usc, which will use the NetDriver using the System.Console. Thanks.

Expected behavior

I expected the console doesn't crash if height is equal or almost equal to zero. With almost zero I mean when the height is less than one and greater than zero. If it can't read with this size then it must check first for its size and not try to read any keystroke or mouse movements.

Actual behavior

The console really crash and disappear from the desktop, causing the errors already mentioned and others like pipe error.

Regression?

No response

Known Workarounds

No response

Configuration

Net6.0, Net7.0
OS: Windows 11 Pro Version: 22H2 Compilation: 22621.1105
Architecture: x64

Other information

No response

Author: BDisp
Assignees: -
Labels:

area-System.Console, area-System.Runtime, untriaged

Milestone: -

@adamsitnik
Copy link
Member

adamsitnik commented Aug 1, 2023

when I resize the console height to zero or almost zero

Currently this scenario is not supported, as Console.WindowHeight throws an exception for 0 and accepts only integers:

public static int WindowHeight
{
get { return ConsolePal.WindowHeight; }
set
{
if (Console.IsOutputRedirected)
{
throw new IOException(SR.InvalidOperation_SetWindowSize);
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);

Cannot read keys when either application does not have a console or when console input has been redirected

Could you provide full stack trace? This error most likely comes from native windows sys-call, but we need to verify that to be 100% sure that it's by design and caused by the OS.

@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label Aug 1, 2023
@adamsitnik adamsitnik modified the milestones: 9.0.0, Future Aug 1, 2023
@BDisp
Copy link
Author

BDisp commented Aug 4, 2023

Thanks for your feedback. Actually on Windows 11 I only having issue running ncurses on wsl.exe when I resize the height to 0. I know it's not possible but it's ncurses that handles it's size and not me. I only I'm resizing with mouse. Maybe some control to the System.Console avoiding have a height of 0.
This is the output:

The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program '[8212] wsl.exe' has exited with code 11 (0xb).
The program '[500] dotnet' has exited with code 0 (0x0).

But there many Windows 10 users complaining about this issue with any .NET app . It seems there was a fix on Windows 11 but not for Windows 10.
Since now I only have Windows 11 I can't reproduce it.

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
Projects
None yet
Development

No branches or pull requests

4 participants