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.ReadKey() Error #30917

Closed
YaraZakaria opened this issue May 9, 2019 — with docs.microsoft.com · 10 comments
Closed

Console.ReadKey() Error #30917

YaraZakaria opened this issue May 9, 2019 — with docs.microsoft.com · 10 comments

Comments

Copy link

Hi, I'm following the steps as they are but i kept getting the following error :

Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.
at System.ConsolePal.ReadKey(Boolean intercept)
at System.Console.ReadKey()
at BasicSendReceiveQuickStart.Program.MainAsync(String ServiceBusConnectionString, String QueueName) in C:\Users\SMQK3813\source\repos\azure-service-bus\samples\DotNet\GettingStarted\BasicSendReceiveQuickStart\BasicSendReceiveQuickStart\Program.cs:line 57
at BasicSendReceiveQuickStart.Program.Main(String[] args) in C:\Users\SMQK3813\source\repos\azure-service-bus\samples\DotNet\GettingStarted\BasicSendReceiveQuickStart\BasicSendReceiveQuickStart\Program.cs:line 34

So I changed the "Console.ReadKey()" in the code to "Console.Read()" and now it works as expected.

Is there a simple explanation for this?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@mike-urnun-msft
Copy link
Contributor

Hello @YaraZakaria Thank you for your feedback! We will review and update as appropriate.

@PramodValavala-MSFT
Copy link
Member

@YaraZakaria I believe Console.ReadKey specifically looks for a key press by the user which will not work when there is no interactive console.
For example, when using input redirection or in a CI job.

On the other hand, Console.Read and Console.ReadLine just obtain the next character / set of characters from the input stream instead, which could be either input from an interactive console or stream redirected to it.

@YaraZakaria
Copy link
Author

@PramodValavala-MSFT So how was I supposed to make it work without changing the code and by using the Console.ReadKey as supposed?

@PramodValavala-MSFT
Copy link
Member

@YaraZakaria Could you share details on the environment where you are running this code like the IDE, OS, etc.

@YaraZakaria
Copy link
Author

@PramodValavala-MSFT I'm running this on my VM having Windows Server 2016 Standard and Visual Studio Entreprise 2019. And the commands in git bash command.

@PramodValavala-MSFT
Copy link
Member

@YaraZakaria There seems to be a couple of inconsistencies when running interactive windows programs that use native windows API in mintty (powers git-bash).

If you are running the dotnet command in CMD or PowerShell, you shouldn't see this problem.

For git-bash, you will have to run to run the dotnet command prefixed with winpty

winpty dotnet BasicSendReceiveQuickStart.dll -ConnectionString "myConnectionString" -QueueName "myQueueName"

Copy link
Author

@PramodValavala-MSFT I tried with winpty and it works as expected.

Thank you

@PramodValavala-MSFT
Copy link
Member

@YaraZakaria Glad we could help.

We will now proceed to close this thread. If there are further questions regarding this matter, please tag me in your reply. We will gladly continue the discussion and we will reopen the issue.

@chucklu
Copy link

chucklu commented Mar 23, 2022

Hi @YaraZakaria , @PramodValavala-MSFT ,
I encounter the same problem, but I don't know how to use winpty on Azure environment. Is there a tutorial or article about how to use winpty on Azure.

@PRMerger6 PRMerger6 added the Pri2 label Mar 23, 2022
@chucklu
Copy link

chucklu commented Mar 23, 2022

Hi @PramodValavala-MSFT ,

If you are running the dotnet command in CMD or PowerShell, you shouldn't see this problem.

We encounter the problem in powershell

2022-03-23T10:03:41.3188553Z Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
2022-03-23T10:03:41.3189560Z    at System.Console.ReadKey(Boolean intercept)
2022-03-23T10:03:41.3190532Z    at UK.Connect.Tool.CopyDll.Program.Main(String[] args) in D:\a\1\s\UK.Connect\tool\UK.Connect.Tool.CopyDll\Program.cs:line 46
2022-03-23T10:03:43.5270171Z ##[error]PowerShell exited with code '1'.
2022-03-23T10:03:43.5773096Z ##[section]Finishing: PowerShell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants