Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Console.Read block in a windows application

Tags:

c#

.net-4.0

I am constantly switching to console mode from windows application mode when debugging. If my application is in Windows mode, will Console.Read and Console.ReadLine wait for user input?

like image 921
Arlen Beiler Avatar asked Aug 26 '26 17:08

Arlen Beiler


2 Answers

In a Windows Application, Console.Read will always return -1, Console.ReadLine will return null, ReadToEnd will return string.Empty, and Peek will return -1;

This is because the Console class will automatically install a custom StreamReader to Console.In so these calls avoid being problematic.

like image 128
Reed Copsey Avatar answered Aug 29 '26 07:08

Reed Copsey


No, they will not. They will act as if they didn't exist. One thing to note, though, is that ReadKey will throw an exception rather than ignore it like the other two. If you need to check whether you are in a console or not see this question.

like image 31
Arlen Beiler Avatar answered Aug 29 '26 07:08

Arlen Beiler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!